Lesson 06
str() — Convert to String
Convert int, float, bool, and complex values into text with str(), and see why converting back follows different rules.
str() converts every fundamental type in this module into text.
Unlike int(), float(), and complex(), converting to string is straightforward — you can convert integers, floats, booleans, and complex numbers without errors.
What you'll learn
- Convert int, float, bool, and complex into strings
- Understand the difference between
print()andrepr() - Use
str()for concatenation and display - Learn why converting back follows different rules
What does str() do?
str() converts a value into a string.
text = str(value)The result is always of type str, regardless of the original type.
Example 1 — Integer → String
a = 100
b = str(a)
print(a, type(a))
print(b, type(b))
print(repr(b))100 <class 'int'>
100 <class 'str'>
'100'Why two different outputs?
print(b)displays the text without quotes.repr(b)shows the actual string representation with quotes.
Example 2 — Float → String
a = 12.34
b = str(a)
print(b)
print(repr(b))12.34
'12.34'The decimal digits are preserved in the string.
Example 3 — Boolean → String
print(str(True))
print(repr(str(True)))True
'True'This creates the text "True", not the boolean value True.
Example 4 — Complex → String
a = 2 + 3j
print(str(a))
print(repr(str(a)))(2+3j)
'(2+3j)'Notice the parentheses — they are part of Python’s string representation for complex numbers.
Interactive playground
Select a value and see how str() converts it.
Choose the source type
Python simulation
str(100)
Conversion successful
- print()
- 100
- repr()
- '100'
print() hides quotes; repr() reveals the actual string.
Convert back with int(): 100 — integer strings can return to integers.
str conversion playground
Conversion map
Every conversion below is valid.
| Original type | Example | String result |
|---|---|---|
| int | str(100) |
'100' |
| float | str(12.34) |
'12.34' |
| bool | str(True) |
'True' |
| complex | str(2+3j) |
'(2+3j)' |
Why str() matters
Strings are required for concatenation.
Incorrect
# "Score: " + 100TypeError: can only concatenate str (not "int") to strCorrect
message = "Score: " + str(100)
print(message)Score: 100Round-trip reminder
Converting to string is easy. Converting back depends on the matching function.
| Original | After str() |
Can convert back? |
|---|---|---|
100 |
"100" |
Yes — int() |
12.34 |
"12.34" |
Yes — float() |
True |
"True" |
No — int() fails |
2+3j |
"(2+3j)" |
No — int() fails; use complex() |
Practice exercises
Exercise 1 — Convert these values into strings.
str(100)
str(12.34)
str(True)
str(2+3j)Print the value and its type.
Exercise 2 — Build this message:
"Mul=" + str(3.5)Expected output: Mul=3.5
Exercise 3 — Predict before running:
int(str(True))Will it succeed or raise an error?
Quick knowledge check
-
Does
str()work for all four types?
Yes. It converts int, float, bool, and complex into text. -
What is
str(True)?
'True'(a string) -
What is
str(2+3j)?
'(2+3j)'
Summary
str()converts all fundamental types into strings.print()displays text without quotes;repr()reveals the actual string.- Use
str()for concatenation, logging, or displaying values. - Converting back requires the matching caster (
int(),float(), orcomplex()), and not every string can be parsed.
Next module: Operators and Expressions — how values combine after you have the right types.
Loading playground…