Error When Trying to Print a Concatenated String


#1

What is the proper way to do this in Python 3? I am getting a type error when running print("The value of pi is around ") + str(3.14) in PyCharm. Thanks.


FAQ: Learn Python – Strings & Console Output – Explicit String Conversion
#2

Hi @murpheylawe89,

Everything you want printed needs to be between the print(...) parentheses. Right now, you’ve got the second part trailing outside the print() statement.

You can also do it this way:

print("The value of pi is around {}".format(str(3.14)))

#3

Bear in mind that format() is already casting to string. We only need to tell it what type to expect.

>>> from math import pi
>>> print("The value of pi is around {:f}".format(pi))
The value of pi is around 3.141593
>>>