9

When combining a variable and a string to be printed in Python, there seem to be a number of ways to do the same thing;

test = "Hello" print "{} World".format(test) #Prints 'Hello World' print test+" World" #Prints 'Hello World' print "%s World" % test #Prints 'Hello World' 

What (if any) is the difference between these methods in terms of performance, compatibility and general preference. Even between open source projects all three methods seem to be used interchangeably.

3
  • 1
    Why would you be concerned about the performance... of a print statement? How much are you going to be printing? Haha. They're all pretty much the same. Use whatever works. Commented Aug 5, 2011 at 17:44
  • 2
    @awfullyjohn I guess it's more of a theoretical question than a practical one, I was just interested if there was any history behind the different methods Commented Aug 5, 2011 at 17:46
  • Please read codinghorror.com/blog/2009/01/… for your concerns about performance on this topic (python and C# have similar implementations of strings, so the conclusions apply equally) Commented Aug 5, 2011 at 17:59

3 Answers 3

9

As you mentioned, various open source projects will use all of these methods for string formatting. However, I would stick to one method for one project so as not to confuse other developers with differing styles.

print test+" World" is the most efficient, performance-wise, but gives you the least amount flexibility

print "%s World" % test #Prints 'Hello World' is basically like C's sprintf which does string interpolation. I like to use this method a lot, because you can pass in not just a regular string, but a dictionary.

print "Good morning %(name), there are %(count)d new articles in %(topic)s today. Would you like to <a href='%(url)s'>read them</a>?" % values

I haven't used "{} World".format(test) personally.

In real applications, the performance difference between these methods are insignificant, and it's really about adhering to style and not over-coding.

Sign up to request clarification or add additional context in comments.

Comments

8

A little benchmark:

>>> a = lambda: "{} World".format("Hello") >>> b = lambda: "Hello" + " World" >>> c = lambda: "%s World" % "Hello" >>> d = lambda: "".join(("Hello", " World")) >>> a(), b(), c(), d() ('Hello World', 'Hello World', 'Hello World', 'Hello World') >>> timeit.timeit(a) 0.7830071449279785 >>> timeit.timeit(b) 0.18782711029052734 >>> timeit.timeit(c) 0.18806695938110352 >>> timeit.timeit(d) 0.3966488838195801 

Seems like b and c are fastest, after d, an surprisingly a is slowest.

But, if you don't do a lot of processing, it doesn't really matter which one to use, just it is better not to mix them.

I personally prefer "".join for just simple concentenations, and str.format for placing values, like "Hello, my name is {}.".format(name).

There was a rumor that % formatting will be deprecated and removed in Python 3, but it didn't.

3 Comments

This is very misleading. Your conclusions only apply to small numbers of short strings, not for types other than strings, longer strings, or more strings. As the number and length of substrings changes, the relative speed will change, especially with b getting slower as the strings get longer or more numerous. Also, b and d require whatever you're concatenating to be strings, while a and c don't. While a and c are roughly similar, a is the most powerful.
What package is timeit.timeit in? I looked on Pypi but couldn't find anything, it looks useful
@samarudge, timeit :). Just import timeit; timeit.timeit(...)
1

As far as I know, the third item has been deprecated for the first item as explained in the python docs. It is removed in Python 3.x and up. The second is really two statements together, a string concatenation and a print statement of the string.

Update:

It seems my info is a little off. From the what's new in python 3.0 page:

PEP 3101: Advanced String Formatting. Note: the 2.6 description mentions the format() method for both 8-bit and Unicode strings. In 3.0, only the str type (text strings with Unicode support) supports this method; the bytes type does not. The plan is to eventually make this the only API for string formatting, and to start deprecating the % operator in Python 3.1.

1 Comment

"%s" % x has not been removed in Python 3, and probably never will be.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.