Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner12 min

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.

str(100) becomes the text '100'

What you'll learn

  • Convert int, float, bool, and complex into strings
  • Understand the difference between print() and repr()
  • Use str() for concatenation and display
  • Learn why converting back follows different rules

What does str() do?

str() converts a value into a string.

python
text = str(value)

The result is always of type str, regardless of the original type.

Example 1 — Integer → String

python
a = 100
b = str(a)

print(a, type(a))
print(b, type(b))
print(repr(b))
text
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

python
a = 12.34
b = str(a)

print(b)
print(repr(b))
text
12.34
'12.34'

The decimal digits are preserved in the string.

Example 3 — Boolean → String

python
print(str(True))
print(repr(str(True)))
text
True
'True'

This creates the text "True", not the boolean value True.

Example 4 — Complex → String

python
a = 2 + 3j

print(str(a))
print(repr(str(a)))
text
(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

python
# "Score: " + 100
text
TypeError: can only concatenate str (not "int") to str

Correct

python
message = "Score: " + str(100)
print(message)
text
Score: 100

Round-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.

python
str(100)
str(12.34)
str(True)
str(2+3j)

Print the value and its type.

Exercise 2 — Build this message:

python
"Mul=" + str(3.5)

Expected output: Mul=3.5

Exercise 3 — Predict before running:

python
int(str(True))

Will it succeed or raise an error?

Quick knowledge check

  1. Does str() work for all four types?
    Yes. It converts int, float, bool, and complex into text.

  2. What is str(True)?
    'True' (a string)

  3. 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(), or complex()), and not every string can be parsed.

Next module: Operators and Expressions — how values combine after you have the right types.

Loading playground…