[python] Print Combining Strings and Numbers

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}

This question is related to python python-2.7

The answer is


if you are using 3.6 try this

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

Output: User pressed the: 250


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}")

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

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)