Print Combining Strings and Numbers

385,602

Solution 1

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.

Solution 2

if you are using 3.6 try this

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

Output: User pressed the: 250

Solution 3

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)

Solution 4

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

Solution 5

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}")
Share:
385,602

Related videos on Youtube

darksky
Author by

darksky

C, C++, Linux, x86, Python Low latency systems Also: iOS (Objective-C, Cocoa Touch), Ruby, Ruby on Rails, Django, Flask, JavaScript, Java, Bash.

Updated on July 05, 2022

Comments

  • darksky
    darksky over 1 year

    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}
    
    • darksky
      darksky over 11 years
      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.
    • DSM
      DSM over 11 years
      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.
    • darksky
      darksky over 11 years
      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.
    • deadcode
      deadcode about 6 years
      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.
    • UselesssCat
      UselesssCat over 5 years
      please add python 2.* tag :(
  • Levon
    Levon over 11 years
    @flep .format also works in 2.6.5 (but it requires {0} {1} for the above example)
  • Levon
    Levon over 11 years
    @Darksky I updated my answer with another way (1b) in case you are specifically interested in "parameterized" prints
  • eqb
    eqb over 8 years
    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
  • Sandun
    Sandun over 3 years
    @Levon the second method gives a Syntax Error 'Missing Parantheses'. Maybe it is no longer supported? I am using Python 3.8.5
  • Muhammad Mohsin Khan
    Muhammad Mohsin Khan almost 2 years
    A code-only answer is not high quality. 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.
  • finn
    finn almost 2 years
    @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.