GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Python str() function - GeeksforGeeks Python str() functionLast Updated : 15 Jul, 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.
Output 123 Explanation: Here, integer 123 is passed to str() function, which returns the string '123'. Syntax
Parameters:
Return Type:
Examples of str() functionExample 1. Converting an Integer to String
Output 123 <class 'str'> Explanation: The integer 123 is converted into the string '123'. Example 2. Convert a Float to a String
Output 3.14159 Explanation: The floating-point number 3.14159 is transformed into the string '3.14159'. Example 3. Converting a List to a String
Output [1, 2, 3] Explanation: The list [1, 2, 3] is converted into the string representation '[1, 2, 3]'. Example 4. Converting a Dictionary to a String
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.
Output Python programming 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.
Output 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. 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.
Output 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. Example 8. Raising an Error on Invalid Bytes with errors='strict'Using errors='strict' raises a UnicodeDecodeError for any invalid byte sequence in the specified encoding, ensuring data conversion accuracy.
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:
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |