overview of str()
The str() function returns a string of the given object. It can be used to change the type of the specific object. For example, a number can be changed from integer to string.
use of str()
The str() function can be used to convert other data types to string values.
# use of str() function
str(5)
str(2.5)
The integer value 5 and the float value 2.5 are converted to the string values '5' and '2.5'.
'5'
'2.5'
syntax of str()
The syntax of the str() function is:
str([object, encoding='utf-8', errors='strict'])
arguments of str()
The str() function accepts up to three arguments, none of them is required. If the object argument is not provided, an empty string is returned.
argument |
required |
description |
object |
optional |
The object, which is to be converted to a string value, the default value is an empty string. |
encoding |
optional |
the encoding of the given object, default value is 'utf-8' |
errors |
optional |
the error handling when the decoding fails, default value is 'strict'. |
By default, the error handling is strict, which means that an UnicodeDecodeError exception is raised.
return values of str()
The str() function returns a string of the given object. If no object is given, an empty string is returned.