vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns str() in Python 2026: String Conversion + Modern Formatting & Best Practices type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns super() in Python 2026: Method Resolution & Modern Inheritance Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices round() in Python 2026: Rounding Numbers + Modern Precision & Use Cases reversed() in Python 2026: Reverse Iteration + Modern Patterns & Best Practices repr() in Python 2026: Official String Representation + Modern Debugging & Serialization Use Cases pow() in Python 2026: Power & Modular Exponentiation + Modern Use Cases & Best Practices property() in Python 2026: Properties, Getters/Setters & Modern Patterns ord() in Python 2026: Unicode Code Point from Character + Modern Use Cases & Best Practices open() in Python 2026: File Handling + Modern I/O Patterns & Best Practices range() in Python 2026: Efficient Sequence Generation + Modern Patterns & Best Practices print() in Python 2026: Output Formatting + Modern CLI & Debugging Patterns View all in this category →

str() in Python 2026: String Conversion + Modern Formatting & Best Practices

Built In Function Mar 24, 2026

In Python, the str() function is a built-in function used to convert an object into a string. This function can be used to convert integers, floats, lists, tuples, dictionaries, and other data types to string data type.

Syntax:

The syntax of the str() function is as follows:

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

Here, object is the object that needs to be converted to a string. The optional encoding and errors parameters specify the encoding to be used for the conversion and the error handling mechanism to be used if the conversion fails.

Examples:

Let's take a look at some examples of using the str() function:

  1. Convert an integer to a string:
x = 42
str_x = str(x)
print(str_x)

 

Output:

'42'

In this example, we convert the integer 42 to a string using the str() function.

  1. Convert a list to a string:
my_list = [1, 2, 3, 4, 5]
str_list = str(my_list)
print(str_list)

 

Output:

[1, 2, 3, 4, 5]

In this example, we convert a list to a string using the str() function.

  1. Convert a tuple to a string:
my_tuple = (1, 2, 3, 4, 5)
str_tuple = str(my_tuple)
print(str_tuple)

 

Output:

(1, 2, 3, 4, 5)

In this example, we convert a tuple to a string using the str() function.

  1. Convert a dictionary to a string:
my_dict = {'name': 'John', 'age': 30}
str_dict = str(my_dict)
print(str_dict)

 

Output:

{'name': 'John', 'age': 30}

In this example, we convert a dictionary to a string using the str() function.

Conclusion:

In conclusion, the str() function is a useful built-in function in Python that can be used to convert various data types to string data type. It is commonly used in print statements, file writing, and other string manipulations in Python.

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices