Difference in printing in Python

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

When will you use this:

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

      

And when do you use it?

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

      

And why?

+3


source to share


3 answers


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

Let's consider first print "The object is an " , X

. 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. In Python 2, the operator takes comma-separated expressions and outputs them one by one, converting them to strings if necessary and using several rules to decide whether to put a space before each expression (it puts a space except for "(1) when characters have not yet been written to stdout, (2) when the last character written to stdout is a space character except '' or (3) when the last write to stdout was not a print expression. ")

So, when you have lines X and Y and do print X,Y

, it prints X and then Y, possibly with a space in between. If you want to print a bunch of things quickly, this works well. This is a somewhat light shorthand for concatenating individual lines. However, it just prints out the string representations of the expressions that you insert. Unless you have already turned the objects into the string you want, you have no control over how they look. This is also what is characteristic of the print operator. In the meantime, there is no need to know about it. ”

The %

string formatting operation , on the other hand, is its own property; 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 by substituting X. But that's not all it can do, or it won't be called string formatting. Instead, it lets you control how things fit into the string, especially for numbers. This makes it more useful even if the usage is a little 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. 

      

However, you should be aware that% formatting is no longer considered the "correct" way of formatting strings, and it is not in Python 3 at all. Instead, both Python 2.6 and up and Python 3 have a method .format

for strings
that is less compact but suitable for the rest python is much better (% is actually an overloaded modular 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 highly recommend reading the documentation on it. Unfortunately, this has what I feel, some bad design choices, and the documentation is pretty opaque.

+5


source


The best example of when you use the first method (percentage formatting) would be

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

      

This makes it easy to insert variables into a string without having to do something like this:



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

      

So, this is personal preference, but percentage formatting allows you to insert variables into a string without concatenation.

+2


source


The first one prints one formatted line. The latter prints two things, one after the other, separated by a space. Use string formatting when you want to assemble a string, for example, for use in a GUI element or as an argument to some processing function. Sending multiple objects to a statement print

(or to a function print()

in Python 3) is mainly for print debugging (although there is nothing wrong with using this program on the command line, as long as the resulting code is the same as what you create with string formatting).

0


source







All Articles