Converts a value to a string (text).
Synopsis
str(value)
Description
You need str to join a number to text with +. An f-string is often easier: it converts for you.
Parameters
| Name | Meaning |
|---|---|
value |
Any value. |
Return value
A str.
Example
age = 14
print("I am " + str(age) + " years old")
print(str(3.5) + "!")
It prints:
I am 14 years old
3.5!
Common mistake
age = 14
print("I am " + age + " years old")
Python stops with:
TypeError: can only concatenate str (not "int") to str
+ can join two strings or add two numbers, but not one of each. Convert with str, or use an f-string.
See also
Last updated 2 weeks ago