![]() |
VOOZH | about |
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.
123
Explanation: Here, integer 123 is passed to str() function, which returns the string '123'.
str(object, encoding='utf-8', errors='strict')
Parameters:
Return Type:
123 <class 'str'>
Explanation: The integer 123 is converted into the string '123'.
3.14159
Explanation: The floating-point number 3.14159 is transformed into the string '3.14159'.
[1, 2, 3]
Explanation: The list [1, 2, 3] is converted into the string representation '[1, 2, 3]'.
{'name': 'Alice', 'age': 25}
Explanation: The dictionary {'name': 'Alice', 'age': 25} is represented as a string.
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 programming
Explanation: str(bd, encoding='utf-8') decodes the byte string using UTF-8.
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.
Hi
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.
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.
Hello world ��
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.
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.
Read articles: