The Python str() function in converts almost any object into its string representation. It is commonly used for displaying values, concatenating text with numbers, and generating messages, logs, or reports.
In simple terms, whenever Python needs to turn data into human-readable text, str() is the go-to function.
This beginner-friendly guide explains how Python str() function converts various data types—such as integers, floats, booleans, lists, tuples, and dictionaries—into readable string formats.
Through clear examples and practical use cases, you’ll learn how to handle type conversion efficiently and avoid common pitfalls when working with Python data.
Note: For converting data types beyond strings, refer to Python Explicit Type Casting techniques.
Syntax of Python str() Function
str(object='')
Parameter Description:
| Aspect | Details |
|---|---|
| Parameter | object — Any Python object to convert into a string. If omitted, returns an empty string. |
| Returns | str — String representation of the object. Returns an empty string if no object is provided. |
| Error Handling | TypeError — Raised if the object cannot be converted into a string (rare for standard Python objects). |
Python str() Function in Action: Examples
Example 1. Converting an Integer to String
num = 100
print(str(num)) # Output: "100"
Explanation: The integer 100 is converted to the string "100", making it safe to combine with other text. Example 2. Converting a Float to String
pi = 3.1415
print(str(pi)) # Output: "3.1415"
Explanation: Decimal numbers are converted into string format for display or concatenation without losing precision.
Example 3. Converting Boolean Values
print(str(True)) # Output: "True"
print(str(False)) # Output: "False"
Explanation: Boolean values are converted into their textual representation "True" or "False".
Example 4. Converting None
value = None
print(str(value)) # Output: "None"
Explanation: The None object is converted to the string "None".
Example 5. Converting Lists, Tuples, and Dictionaries
my_list = [1, 2, 3]
my_tuple = (4, 5)
my_dict = {"a": 1}
print(str(my_list)) # Output: "[1, 2, 3]"
print(str(my_tuple)) # Output: "(4, 5)"
print(str(my_dict)) # Output: "{'a': 1}"
Explanation: Collections are converted into readable string format, preserving their structure.
Example 6. Concatenating Strings with Numbers
score = 50
message = "Your score is " + str(score)
print(message) # Output: "Your score is 50"
Explanation: Without str(), Python would raise a TypeError. Using str() ensures smooth concatenation.
Example 7. Ignoring Extra Spaces or Special Characters
text = " 123 "
print(str(text)) # Output: " 123 "
Explanation: Strings with leading/trailing spaces or special characters remain intact when passed to str().
Example 8: Invalid Scenarios
# str() works with almost any object, so invalid conversions are rare
# The main issue arises when concatenation is attempted without str()
age = 25
print("Your age is " + age) # Raises TypeError
If you forget to convert non-string objects while combining with text, Python raises a TypeError. Always use str() when combining text with numbers, booleans, or other types.
Using str() Inside print() vs Assigned Conversion
Python’s str() function can be used either inline inside a print() statement or assigned to a variable first.
Inline conversion is quick for one-time display, while assignment is better when the string will be reused, manipulated, or passed to other functions.
Example 1: Inline Conversion in print()
temperature = 37.5
print("Current temperature: " + str(temperature) + "°C") # Output: Current temperature: 37.5°C
Explanation: The float temperature is converted to a string only for printing. The variable itself remains a float.
Example 2: Assigned Conversion Before Use
temperature = 37.5
temp_str = str(temperature) # Convert float to string and assign to variable
print("Current temperature: " + temp_str + "°C") # Output: Current temperature: 37.5°C
print("Temperature logged: " + temp_str) # Output: Temperature logged: 37.5
Explanation: By storing the string in temp_str, it can be reused multiple times without repeatedly calling str(). This approach is useful for logging, reporting, or repeated concatenation.
Pro Tip: Use inline conversion for one-off prints. Use assigned conversion when the string will be reused to improve readability and reduce repeated function calls.
Summary Table of str() Conversions
| Input Type | Example | Output |
|---|---|---|
| Integer | 100 | “100” |
| Float | 3.1415 | “3.1415” |
| Boolean | True / False | “True”/”False” |
| None | None | “None” |
| List | [1, 2, 3] | “[1, 2, 3]” |
| Tuple | (4, 5) | “(4, 5)” |
| Dictionary | {“a”: 1} | “{‘a’: 1}” |
| String (already string) | “Hello” | “Hello” |
str() function: