Python str() function (original) (raw)

Last Updated : 01 May, 2025

The **str() function in Python is an in-built function that takes an **object as **input and returns its **string representation. It can be used to convert various data types into strings, which can then be used for printing, concatenation, and formatting. Let’s take a simple example to **converting an Integer to string.

Python `

n = 123 s = str(n) print(s)

`

*Explanation: Here, integer 123 is passed to str() function, which returns the string ‘123*‘.

Syntax

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

**Parameters:

**Return Type:

Examples of str() function

Example 1. Converting an Integer to String

Python `

n = 123 s = str(n) print(s, type(s))

`

Explanation: The integer 123 is converted into the string ‘123**‘.

Example 2.Convert a Float to a String

Python `

pi = 3.14159 s = str(pi) print(s)

`

**Explanation: The floating-point number **3.14159 is transformed into the string ‘3.14159’.

Example 3. Converting a List to a String

Python `

a = [1, 2, 3] s = str(a) print(s)

`

**Explanation: The list **[1, 2, 3] is converted into the string representation ‘[1, 2, 3]’.

Example 4. Converting a Dictionary to a String

Python `

b = {'name': 'Alice', 'age': 25} s = str(b) print(s)

`

Output

{'name': 'Alice', 'age': 25}

**Explanation: The dictionary {‘name’: ‘Alice’, ‘age’: 25} is represented as a string.

Example 5. Converting a Byte Object to String (with Encoding)

In this example, we’ll convert a **byte object representing **text into a string. By specifying the encoding, **str() can correctly interpret the byte data as text.

Python `

bd = b'Python programming'

txt = str(bd, encoding='utf-8') print(txt)

`

Explanation: str(bd, encoding=’utf-8′) decodes the byte string using UTF-8.

Example 6. Handling Decoding Errors with errors=’ignore’

Sometimes, **byte data may contain invalid characters or sequences for a given encoding. The errors parameter allows us to control how these issues are handled. Using errors=’ignore’, we can skip invalid characters during conversion.

Python `

bd = b'Hi \x80\x81'

t = str(bd, encoding='utf-8', errors='ignore') print(t)

`

**Explanation: Here, **\x80 and \x81 are invalid UTF-8 byte sequences. By setting **errors=’ignore’, these invalid characters are simply omitted from the output, resulting in a clean, readable string.

Example 7. Handling Decoding Errors with errors=’replace’

Using **errors=’replace’, we can substitute any problematic characters with a placeholder symbol (often a question mark, ****?**), so it’s clear where data was altered during conversion.

Python `

bd = b'Hello world \x80\x81'

t = str(bd, encoding='utf-8', errors='replace') print(t)

`

**Explanation: invalid byte sequences **\x80 and \x81 are replaced by ****?**, indicating that there were unrecognized characters in the data. This is helpful when we want to keep the original format of text without causing any errors when reading it.

Using errors=’strict’ raises a **UnicodeDecodeError for any invalid byte sequence in the specified encoding, ensuring data conversion accuracy.

Python `

bd = b'Hi \x80\x81'

try: t = str(bd, encoding='utf-8', errors='strict') print(t) except UnicodeDecodeError as e: print("Error:", e)

`

Output

Error: 'utf-8' codec can't decode byte 0x80 in position 3: invalid start byte

**Explanation: **byte_data_invalid contains invalid **UTF-8 sequences (**\x80 and \x81). Using **errors=’strict’ causes Python to raise a **UnicodeDecodeError, stopping the conversion and providing an error message.

Common Use Cases:

**Read articles: