Python str() Function Explained
The Python str() function converts a value into a string.
A string is text in Python. Beginners often use str() when they need to turn a number into text before combining it with other strings.
Quick example
age = 25
text = str(age)
print(text)
print(type(text))
Output:
25
<class 'str'>
Use str() when you need a string version of a value, such as a number before joining or concatenating text.
What str() does
str() converts a value into a string.
That means:
- An integer like
10becomes"10" - A float like
3.14becomes"3.14" - A boolean like
Truebecomes"True" - A list like
[1, 2, 3]becomes"[1, 2, 3]"
The result is text, so you can:
- print it
- combine it with other strings
- save it to a file
- use it in messages
Basic syntax
The basic syntax is:
str(object)
Here, object is the value you want to convert.
str() always returns a string.
A very common beginner use case is converting a number before combining it with text:
score = 100
message = "Your score is " + str(score)
print(message)
Output:
Your score is 100
If you are not sure what type a value has, the type() function can help you check.
Simple examples
Convert an integer to a string
number = 10
text = str(number)
print(text)
print(type(text))
Output:
10
<class 'str'>
Convert a float to a string
price = 3.14
text = str(price)
print(text)
print(type(text))
Output:
3.14
<class 'str'>
Convert a boolean to a string
value = True
text = str(value)
print(text)
print(type(text))
Output:
True
<class 'str'>
Convert a list to a string
items = [1, 2, 3]
text = str(items)
print(text)
print(type(text))
Output:
[1, 2, 3]
<class 'str'>
This gives you the string form of the list. It does not join the items into a clean sentence.
When to use str()
Use str() when you want a text representation of another data type.
Common cases:
- Before concatenating text with numbers
- Before writing mixed values to a file
- Before displaying values in messages
- When you want text instead of a numeric value
Example:
name = "Maya"
age = 25
message = name + " is " + str(age) + " years old."
print(message)
Output:
Maya is 25 years old.
If you need help with cleaner message building, see how to format strings in Python.
str() vs print()
These two functions do different jobs:
str()converts a value and returns a stringprint()displays output on the screen
Example:
value = 42
text = str(value)
print(text)
print(type(text))
print(type(value))
Output:
42
<class 'str'>
<class 'int'>
Notice:
str(value)created a stringprint()only displayed valuesprint()did not changevaluefrominttostr
You can learn more about output with the print() function.
str() vs repr() for beginners
For beginners, the main idea is simple:
str()is the more readable text versionrepr()is more developer-focused
In everyday beginner code, you will usually need str() much more often.
For example, when showing values in a message, str() is usually the right choice.
Common beginner mistakes
Thinking str() changes the original variable
str() does not modify the original value. It returns a new string.
age = 25
text = str(age)
print(age)
print(type(age))
print(text)
print(type(text))
Output:
25
<class 'int'>
25
<class 'str'>
Forgetting to store the result
This works:
age = 25
text = str(age)
But if you only do this:
age = 25
str(age)
the conversion happens, but you did not save the result anywhere.
Using str() too early
If you convert a number to a string, you can no longer use it directly for math.
price = 10
text_price = str(price)
# print(text_price + 5) # This would cause an error
If you need a number again, convert it back with int() or float(), depending on the situation.
Expecting str(list_value) to create user-friendly formatting
This:
items = ["apple", "banana", "orange"]
print(str(items))
prints:
['apple', 'banana', 'orange']
That is the string representation of the list. It is not the same as a nicely formatted sentence.
Return value
str() returns a new string.
Important points:
- It always returns a string
- It does not modify the original object
- You can use
type()to confirm the result
Example:
value = 99
converted = str(value)
print(converted)
print(type(converted))
Output:
99
<class 'str'>
Common causes of confusion
Beginners usually run into trouble with str() for these reasons:
- Trying to add a string and an integer without conversion
- Confusing conversion with printing
- Using
str()too early and then trying to do math on the result - Assuming list or dictionary conversion creates user-friendly formatting
A very common related error happens when you try to combine text and a number directly. If that is your problem, see how to fix TypeError: can only concatenate str not int to str.
Useful debugging steps
If you are not sure what is happening, print both the value and its type:
value = 25
print(value)
print(type(value))
converted = str(value)
print(converted)
print(type(converted))
This helps you see exactly when a value changes from int, float, or another type into str.
FAQ
What does str() return in Python?
It returns a string version of the value you pass in.
Does str() change the original variable?
No. It creates and returns a new string.
Can str() convert numbers to text?
Yes. It can convert integers, floats, and other values into strings.
Why use str() before combining text and numbers?
Because Python does not let you directly concatenate a string with an integer or float.
Is str() the same as print()?
No. str() converts to text. print() shows output.