Python Reference Library →

Python str() Function – Convert to String Easily

Let’s look at str() function – convert to string easily

  1. Notice

    str() produces a human-readable string representation of an object.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 3
text = str(number)
print(text)
print(type(text).__name__)
Live state

Top-level scope

number→Object 1
text→Object 2
Object 1 · int3Object 2 · str'3'
This step

str() produces a human-readable string representation of an object.

Picture str() function – convert to string easily

str() produces a human-readable string representation of an object.

Image brief

Alt text
A diagram for Python str() Function – Convert to String Easily. Instructions: number = 3 text = str(number) print(text) print(type(text).__name__). Displayed result: 3 str. The final example bindings are number --> object 1 (int); text --> object 2 (str). object 1: 3; object 2: '3'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python str() Function – Convert to String Easily". Teaching focus: str() produces a human-readable string representation of an object. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 3 text = str(number) print(text) print(type(text).__name__) | v DISPLAYED RESULT 3 str FINAL BINDINGS number --> object 1 (int) text --> object 2 (str) object 1: 3 object 2: '3' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python str() Function – Convert to String Easily. Instructions: number = 3 text = str(number) print(text) print(type(text).__name__). Displayed result: 3 str. The final example bindings are number --> object 1 (int); text --> object 2 (str). object 1: 3; object 2: '3'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 3
text = str(number)
print(text)
print(type(text).__name__)
Live state

Top-level scope

number→Object 1
text→Object 2
Object 1 · int3Object 2 · str'3'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3
str

What do you expect this program to print?

What does this example display?

number = 3
text = str(number)
print(text)
print(type(text).__name__)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3 str

Follow the printed values in order. The output is: 3 str

More examples and reference

Python str() Function

The str() function in Python is used to convert values into a string format. Whether it’s a number, a list, or an object, str() gives you the string version of it.

Syntax

str(object='', encoding='utf-8', errors='strict')

Parameters:

  • object (optional) – The object you want to convert to a string.
  • encoding (optional) – Only used if the object is a bytes-like object.
  • errors (optional) – Specifies how to handle encoding errors.

Returns:

  • A string version of the specified object.

Example 1: Converting Numbers to Strings

x = 123
print(str(x))
'123'

Example 2: Converting a List to a String

my_list = [1, 2, 3]
print(str(my_list))
'[1, 2, 3]'

Example 3: Using str() with a Float

pi = 3.14159
print(str(pi))
'3.14159'

Use Case: String Concatenation

Sometimes you want to add text and numbers together. You need to convert the number into a string first:

name = "Alice"
age = 25
print("Name: " + name + ", Age: " + str(age))
Name: Alice, Age: 25

Special Case: Encoding and Decoding Bytes

b = b"hello"
print(str(b, encoding="utf-8"))
'hello'

Common Mistakes

  • Forgetting to convert before concatenation: Trying to join strings with numbers directly will cause a TypeError.
  • Using str as a variable name: This overwrites the built-in str() function. Avoid this!

Interview Tip

When asked to serialize, log, or print object details in coding interviews, str() is a quick and reliable tool.

Summary

  • str() converts values to string format.
  • Useful in printing, concatenation, and formatting.
  • Supports numbers, lists, dictionaries, and user-defined objects.

Practice Problem

Write a program that reads your name and age, and prints a greeting using str().

name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello " + name + "! You are " + str(age) + " years old.")

What to remember

Connect what you noticed in the example with the idea below.

Remember

str() produces a human-readable string representation of an object.

Watch out for this idea

str() modifies the source object’s type in place.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →