1

I've come across lines like this in Python:

print "Let's talk about %s." % my_name

However, I've seen cases in Ruby where these two have equivalent output:

print "Let's talk about #{my_name}."

print "Let's talk about %s." % my_name"

Is there only one way to print the same line in Python?

halfer
  • 19,824
  • 17
  • 99
  • 186
stanigator
  • 10,768
  • 34
  • 94
  • 129

5 Answers5

6

In Python there are also several ways:

print "let's talk about this %s" % my_name
print "let's talk about this {:s}".format(my_name)
print "let's talk about this", my_name

all generate the same output. However, use of the .format method (available since Python v 2.6) is encouraged over the old-style % (To quote from the What's New in 3.0: "...the % operator is still supported; it will be deprecated in Python 3.1 and removed from the language at some later time.")

Lastly, as of Python v3.x print is a function, so all print statements require ().

Levon
  • 138,105
  • 33
  • 200
  • 191
2

In ruby, the part between the {} is an expression. Python has no way to evaluate expressions inside the format string this way.

print "Let's talk about #{my_name}."

In the simple case where the expression in just a variable, you can use

print "Let's talk about %(my_name)s"%{'my_name':my_name}

or any of the bunch of other alternatives given in other answers here.

The preferred way is to use format(). eg.

print "Let's talk about {my_name}".format(my_name=my_name)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • I just started to use the .format method .. why would the last print you show be the preferred way? I find it incredibly verbose. I'm not trying to argue, but I don't see the benefit. Perhaps it's just the context of this simple example? Or is there a good reason for this? – Levon May 30 '12 at 02:11
  • @Levon, format() gives you a lot of control over the output - spacing, padding, etc. Using a mapping for substitutions in text output is a good habit, because it makes life much easier if you ever have to do i18n – John La Rooy May 30 '12 at 02:14
0

You could also concatenate strings. This is less pythonic and gives you less control over the final appearance of your output, but some non-programmers seem to find this easier to read (like if you're discussing a specific calculation):

print "Let's talk about " + str( my_name )

The str() function may or may not be necessary, but it allows you to add in other datatypes (like integers) without getting a TypeError.

For more information on string.format() vs %, see this older post: Python string formatting: % vs. .format

Community
  • 1
  • 1
abought
  • 2,652
  • 1
  • 18
  • 13
0

In addition to other answers here, there are also mapping keys, which I took way too long to discover in my life with Python.

>>> '%(foo)s' % {'foo': 'bar'}
'bar'

Have a look at String Formatting Operations for the whole shebang.

Mattie
  • 20,280
  • 7
  • 36
  • 54
0

One of my favorites is to use a dictionary for values:

>>> values = {"name": "john"}
>>> print "Let's talk about {name}".format(**values)
Let's talk about john
monkut
  • 42,176
  • 24
  • 124
  • 155
  • Depends on the extent of the data you need to output via print. For a single line, with a single value it's overkill, but when you start to increase the variables in the output it can be much cleaner. – monkut May 30 '12 at 02:36