An alternative to string interpolation

I sort of like this.

# ugly
msg = "I found %s files in %s directories" % (filecount,foldercount)

# better
def Str(*args): return "".join(str(x) for x in args)
:
:
msg = Str("I found ", filecount, " files in ", foldercount, " directories" )

You don’t have to call it “Str”, of course.

Advertisements
This entry was posted in Python features. Bookmark the permalink.

15 Responses to An alternative to string interpolation

  1. Perl: $msg = “I found $filecount files in $foldercount directories.”;

    😉

  2. ulrik says:

    ‘Str’ version is impossible to translate.

    print(“I found {filecount} files in {foldercount} directories” % {“filecount”: x, “foldercount”: y})

    some hacky people use locals() for the mapping argument, but that can be a maintenance/security liability.

    • ulrik says:

      Oh my, I managed to mix up “%(style1)s” % {“style1”: 1} and “{style2}”.format(**{“style2”: 2}) completely. Shame on me.

  3. Anonymous says:

    Why the hell you want so make so simple task as string joining so complicated?!

  4. mj says:

    I don’t see why the original is considered ugly. It’s shorter and separates the presentation from the values. What happens if you want to pad either number with zeroes or any other format?

  5. multani says:

    Or you can also simply use::

    msg = “I found ” + str(filecount) + ” files in ” + str(foldercount) + ” directories”

    (I always fear the cast to str())

    • Steve Ferg says:

      The advantage of Str() is that it does not do string concatenation — it joins members of a list — so it should be faster.

      The downside of course is that it casts everything to strings, including things that are already strings. But I’m betting that there is hardly any cost to this, that Python is smart enough simply to do nothing at all, when it is asked to cast a string to a string.

      So I think the cost (in speed) of casting everything to strings is more than repaid by avoiding the overhead of string concatenation.

  6. Ben Finney says:

    “I found %(filecount)d files in %(foldercount)d directories” % vars()

  7. Anonymous says:

    I tought
    “I found {} files in {} directories”.format(filecount,foldercount)
    was the python3 way of doing it. Or
    “I found {filecount} files in {foldercount} directories”.format(filecount=filecount,foldercount=foldercount)
    if you want more readability in the string and you dont mind some uglyness at the end…

  8. As others have said in the comments, your solution looks really ugly. In newer versions of python the preferred way of doing it would be (as someone else has already suggested):

    “I found {filecount} files in {foldercount} directories”.format(filecount=filecount,foldercount=foldercount)

  9. yet says:

    Nonsense!

  10. Jj says:

    Not to mention issues you’ll have when calling str() on unicode strings.

  11. Why do you need a function for this? I inline this pattern all the time in JavaScript. It’s not too many characters. If you write it out explicitly each time, new people who have never seen your code should be able to understand it easier.

  12. I should mention I think this all only makes sense for big loops or giant concatenations.

Comments are closed.