- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python str() Function
The str() function in Python is used when we need to convert a value to a string object. For example:
a = 1234 x = str(a) print(type(x)) a = 2.43 x = str(a) print(type(x)) a = [12, 32, 43] x = str(a) print(type(x))
The output is:
<class 'str'> <class 'str'> <class 'str'>
Python str() Function Syntax
The syntax of str() function in Python, is:
str(object, encoding, errors)
where object refers to an object that is going to convert into a str object. The encoding parameter is used to apply the particular encoding method, while converting the object into string. And the errors parameter is used to specify what to do, in case, if decoding fails.
Note: The encoding and errors parameters are optional.
Note: The default value of encoding is UTF-8. And the default value of errors is strict.
Important - If encoding parameter is used to specify the encoding method, then the object must be a bytes object.
The errors parameter's value can be any of the following:
- strict - The default value of errors. It raises an exception named UnicodeDecodeError, on failure
- ignore - This value to errors is used to ignore the unencodable Unicode
- replace - This value is used to replace the unencodable Unicode to a question mark (?)
- xmlcharrefreplace - This value is used to insert an XML character reference, instead of unencodable Unicode
- backslashreplace - This value is used to insert an escape sequence \uNNNN, instead of unencodable Unicode
- namereplace - This value is also used to insert an escape sequence \N{...}, instead of unencodable Unicode
Python str() Function Example
Here is an example of str() function in Python:
a = 12 x = str(a) print(x) a = b'Python Programming' x = str(a, 'ascii') print(x) a = b'Python Programming' x = str(a, 'U7', 'replace') print(x) a = [12, 23, 43] x = str(a) print(x) print(x[0]) print(x[1]) print(x[2]) print(x[3]) print(x[-1])
The snapshot given below shows the sample output produced by above program, demonstrating the str() function in Python:
« Previous Function Next Function »