Skip to main content

How to Convert a Value to a String in Python with str

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

str(value) turns any Python value into text: str(3) is "3", str(True) is "True" and str(None) is "None". You need it whenever you join a value to other text with +, because Python won't mix the two on its own.

Syntax​

Syntax
<text> = str(<value>)
PartWhat it does
<value>Any value: a number, a boolean, None, a list or an object of your own.
str(...)Returns the value as a string, in the form you'd normally print it.

The Python documentation explains that str(object) returns type(object).__str__(object), "which is the "informal" or nicely printable string representation of object."

The Example​

The example joins a number to text with str, converts a decimal number, a boolean, None and a list, and then tries to join text and a number without str.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which prints Apples plus str(count), then str of 2.5, True, None and a list, and finally tries Apples plus count without str.

main.py
count = 3
print("Apples: " + str(count))
print(str(2.5))
print(str(True))
print(str(None))
print(str([1, 2, 3]))
print("Apples: " + count)

Step 2: Run It​

Terminal
uv run python main.py
Output
Apples: 3
2.5
True
None
[1, 2, 3]
Traceback (most recent call last):
File "/Users/Shared/you/main.py", line 7, in <module>
print("Apples: " + count)
~~~~~~~~~~~^~~~~~~
TypeError: can only concatenate str (not "int") to str

A zsh terminal where uv run python main.py prints Apples 3, 2.5, True, None and [1, 2, 3], then a traceback ending in TypeError: can only concatenate str (not "int") to str.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • "Apples: " + str(count) worked, because both sides were strings.
  • Every value had a text form. str wrote the number, the boolean, None and the list the way print shows them.
  • The last line stopped the program. "Apples: " + count asks Python to add a string and an integer, and it refuses with TypeError: can only concatenate str (not "int") to str rather than guessing what you meant.

Common Mistakes​

MistakeWhat happensDo this instead
Joining text and a number with +TypeError: can only concatenate str (not "int") to strWrap the number in str(), or use an f-string
Expecting str(None) to be an empty stringIt's the four letters NoneCheck for None before converting
Converting a value to compare it with text"3" == 3 is False in PythonCompare values of the same type

Troubleshooting​

SymptomLikely causeFix
TypeError: can only concatenate str (not "int") to strA number was joined to text with +Convert it with str()
The word None appears in your outputA variable held None when it was convertedFind where it should have been given a value

FAQ​

Does print need str?​

No. print converts each value itself, which is why print(count) works. You need str() when you build the text yourself.

What's the difference between str and repr?​

str gives the readable form and repr gives the form a programmer would type: str("hi") is hi, while repr("hi") includes the quotes.

Conclusion​

str(value) turns any value into text. Use it whenever you join a value to other text with +.

Additional Resources​