bhsingh
bhsingh

Reputation: 101

Difference in printing in Python

I am learning Python and here is an example of some code :

When would you use this :

Y = "Apple"
print "The object is an %s" % Y

And when would you use this?

X = "Grape"
print "The object is an " , X

And why?

Upvotes: 3

Views: 87

Answers (3)

cge
cge

Reputation: 9890

The difference goes beyond just convenience and preference. The two methods are two different things.

Let's consider print "The object is an " , X first. The print statement in Python 2 is a bit irregular and unintuitive in its behavior, which is one of the reasons why Python 3 has a print function instead. In Python 2, the statement takes comma-separated expressions, and prints them out one by one, converting them to strings if necessary, and using a few rules to decide whether to put a space before each expression (it puts a space except "(1) when no characters have yet been written to standard output, (2) when the last character written to standard output is a whitespace character except ' ', or (3) when the last write operation on standard output was not a print statement.")

So when you have strings X and Y, and do print X,Y, it prints X and then Y, probably with whitespace in between. If you want to print a bunch of things quickly, this works well. It's to some extent an easy shorthand for combining separate strings as well. It just prints string representations of the expressions you put in, however. Unless you've already converted the objects to the string you want them to be, you don't have any control over what they look like. It is also something that's specific to the print statement.

The % operation for string formatting, on the other hand, is its own thing; you don't need to use it with print. You can also do things like a = "The object is an %s." % X, and it will work as expected, substituting in the X. But that's not all it can do, or it wouldn't be called string formatting. Instead, it allows you to control how things are put into the string, especially for numbers. This makes it more generally useful, even if the usage is a bit opaque, and reading the documentation on it is a good idea. But, as some examples:

In [1]: a = 1507.2515621

In [2]: print "a is: %d" % a # print as a signed integer
a is: 1507

In [3]: print "a is: %f" % a # print as a float, decimal format
a is: 1507.251562

In [4]: print "a is: %10.2E" % a # print as a float in exponential format, with
a is:   1.51E+03

In [5]: print "a is: %x" % a # signed hexadecimal
a is: 5e3

In [6]: print "The object is an %s." % "Apple" # a string using str()
The object is an Apple.

In [7]: print "The object is an %r." % "Apple" # a string using repr()
The object is an 'Apple'.

In [19]: z = {'a': 2, 'b': 3}

In [21]: print "a is %(a)d, and b is %(b)d." % z
a is 2, and b is 3. 

You should be aware, however, that % formatting is no longer considered the "correct" way to do string formatting, and it isn't in Python 3 at all. Instead, both Python 2.6 and up, and Python 3 have the .format method on strings, which is less compact, but fits the rest of python much better (% is actually an overloaded modulo operator). As some examples:

In [39]: print "a is: {0}, or {0:g}, or {0:e}, and z is {1:s},\n and a in z is {1[a]}, \
   ....: but the a variable is {0:,}.".format(a,z)
a is: 1507.2515621, or 1507.25, or 1.507252e+03, and z is {'a': 2, 'b': 3},
 and a in z is 2, but the a variable is 1,507.2515621.

This has many options, and I'd highly recommend reading the documentation on it. Unfortunately, it has what I feel are some unfortunate design choices, and the documentation is rather opaque.

Upvotes: 5

TigerhawkT3
TigerhawkT3

Reputation: 49318

The former prints a single, formatted string. The latter prints two things, one after the other, separated by a space. Use string formatting when you want to put together a string, such as for use in a GUI element or as an argument to some processing function. Sending multiple objects to the print statement (or to the print() function in Python 3) is mostly for print debugging (although there's nothing wrong with using it in a command-line program, if the resulting code is as clear as what you'd create with string formatting).

Upvotes: 0

Taylor Glaeser
Taylor Glaeser

Reputation: 1313

A better example of when you would use the first method (percent formatting) would be

Y = 'Apple'
print "The %s tastes sweet." % Y

It allows you to easily insert variables into a string without having to do something like this:

Y = 'Apple'
print "The", Y, " tastes sweet."

So it's personal preference really, but percent formatting allows one to insert variables into a string without concatenation.

Upvotes: 2

Related Questions