83

To print strings and numbers in Python, is there any other way than doing something like:

first = 10
second = 20
print "First number is %(first)d and second number is %(second)d" % {"first": first, "second":second}
Jones1220
  • 786
  • 2
  • 11
  • 22
darksky
  • 20,411
  • 61
  • 165
  • 254
  • 11
    For whoever is voting to close this question: how is this not a real question? It is not difficult to tell what is being asked, the question is not ambiguous, neither vague, incompletely, overly broad or rhetorical. It is simply a question asked by a person who is just starting to learn Python. If you intend to vote to close, please add a comment. – darksky Aug 18 '12 at 13:49
  • 1
    FWIW I've seen people use "not a real question" as a replacement category for "insufficient effort shown" in the past -- the first google result for "python how to print" is the Input/Output section of the Python tutorial, which covers all the methods listed. It doesn't really match, but it's sometimes used as a catch-all. – DSM Aug 18 '12 at 14:36
  • 2
    I obviously asked this very simple question after finishing the whole tutorial on `python.org`. I came across a few days which seemed to complicated and I was sure there were other ways, as you can see below. The `.format` is not listed in the tutorial under Strings but in the library references. – darksky Aug 18 '12 at 15:35
  • 7
    I have come across this behavior too. Questions from beginners are voted to close by supposedly seasoned coders. One thing is that some people learn slower and if there are users willing to answer the question what is the issue? I hope the community gets more accommodating. – deadcode Dec 24 '17 at 15:36
  • please add python 2.* tag :( – UselesssCat May 18 '18 at 23:45

6 Answers6

144

Using print function without parentheses works with older versions of Python but is no longer supported on Python3, so you have to put the arguments inside parentheses. However, there are workarounds, as mentioned in the answers to this question. Since the support for Python2 has ended in Jan 1st 2020, the answer has been modified to be compatible with Python3.

You could do any of these (and there may be other ways):

(1)  print("First number is {} and second number is {}".format(first, second))
(1b) print("First number is {first} and number is {second}".format(first=first, second=second)) 

or

(2) print('First number is', first, 'second number is', second) 

(Note: A space will be automatically added afterwards when separated from a comma)

or

(3) print('First number %d and second number is %d' % (first, second))

or

(4) print('First number is ' + str(first) + ' second number is' + str(second))
  

Using format() (1/1b) is preferred where available.

Sandun
  • 395
  • 2
  • 10
  • 25
Levon
  • 138,105
  • 33
  • 200
  • 191
  • @flep .format also works in 2.6.5 (but it requires {0} {1} for the above example) – Levon Aug 18 '12 at 13:45
  • 1
    @Darksky I updated my answer with another way (1b) in case you are specifically interested in "parameterized" prints – Levon Aug 18 '12 at 13:46
  • In (2), you don't need the extra space in the strings. Putting commas will insert spaces by default. So instead of (2) print 'First number is', first, ' second number is', second do this: (2) print 'First number is', first, 'second number is', second – eqb Sep 21 '15 at 19:28
  • @Levon the second method gives a Syntax Error 'Missing Parantheses'. Maybe it is no longer supported? I am using Python 3.8.5 – Sandun Aug 20 '20 at 08:38
9

if you are using 3.6 try this

 k = 250
 print(f"User pressed the: {k}")

Output: User pressed the: 250

George C.
  • 6,574
  • 12
  • 55
  • 80
7

Yes there is. The preferred syntax is to favor str.format over the deprecated % operator.

print "First number is {} and second number is {}".format(first, second)
Antimony
  • 37,781
  • 10
  • 100
  • 107
6

The other answers explain how to produce a string formatted like in your example, but if all you need to do is to print that stuff you could simply write:

first = 10
second = 20
print "First number is", first, "and second number is", second
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
4

In Python 3.6

a, b=1, 2 

print ("Value of variable a is: ", a, "and Value of variable b is :", b)

print(f"Value of a is: {a}")
0
import random
import string
s=string.digits
def foo(s):
  r=random.choice(s)
  p='('
  p2=')'
  str=f'{p}{r}{p2}'
  print(str)
foo(s)

thank me later

finn
  • 11
  • 2
  • 1
    [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Muhammad Mohsin Khan Mar 14 '22 at 16:47
  • @MuhammadMohsinKhan in this case the question is so old that the answer will most likely go unseen. Also if they do not understand something they can easily ask a question about it. I have used Stack Overflow for many years on other accounts that have been banned, I understand how the platform works. – finn Mar 15 '22 at 15:06