Python str() Function

Returns an object converted to a string.

Syntax

Python
str(object)
str(buffer, encoding = sys.getdefaultencoding(), errors = "strict")

Parameters

ParameterDescription
object Required. The object to be converted to a string.
buffer Required. The buffer to be converted to a string.
encoding Optional. The name of the encoding used to decode the buffer. If encoding is then the first parameter must be a data buffer. Default is sys.getdefaultencoding()
errors Optional. The error handling method to use in case of errors. If errors is supplied then the first parameter must be a data buffer. Default is 'strict'.

Possible values are
  • strict - default response which raises a UnicodeDecodeError exception on failure
  • ignore - ignores the unencodable Unicode from the result
  • replace - replaces the unencodable Unicode to a question mark
  • xmlcharrefreplace - inserts XML character reference instead of unencodable Unicode
  • backslashreplace - inserts a \uNNNN espace sequence instead of unencodable Unicode
  • namereplace - inserts a \N{...} escape sequence instead of unencodable Unicode

Example

Python
aString = str(3.14)
print(aString, ' is a string: ', type(aString) is type('string'))

Output

3.14 is a string: True