The str () function in Python
In Python, the str () function returns the selected object as a string.
The syntax of the str () function in Python
The str () function in Python has the syntax:
str(object, encoding='utf-8', errors='strict')
Parameters of the str () function
The str () function has 3 parameters:
- Object : An object that can be displayed as a string. If not supplied, the result is an empty string.
- encoding : Encoding of an object. If not provided, the default encoding is UTF-8.
- errors : Respond when encoding fails. The default value is 'strict'.
There are 6 types of errors:
- strict : The default response raises the UnicodeDecodeError exception when an error occurs
- ignore : Removes Unicode which cannot be encoded in the result
- replace : Replace non-Unicode encoding with a question mark
- xmlcharrefreplace : Insert an XML character reference instead of Unicode that cannot be encoded
- blackslashreplace : Insert a string uNNNN instead of Unicode which cannot be encoded
- nameraplace : Insert a string N {.} instead of Unicode which cannot be encoded

The return value of the str () function
The str () function returns a string that is considered to be an unofficial or printable representation of an object.
Example 1: Converting an object to a string using str ()
If two encoding and errors parameters are not provided, the str () function calls the internal __str __ () method of an object.
If the __str __ () method cannot be found , it will call the repr (obj) function instead.
For example:
NamePage = str('TipsMake.com') print(NamePage)
When running the program, the result we get is:
TipsMake.com
Note: The resulting variable will contain a string.
Example 2: How does str () function with bytes?
If the encoding and errors parameters are provided, the first parameter - the object - will have to be a byte object (bytes or bytearray).
If the object is in the form of bytes or bytearray , the str () function will call the bytes.decode (ecoding, errors) method .
Besides, it will get the byte object in the buffer before calling the decode () method .
For example
# str() handling objects in the form of bytes b = bytes('TipsMake', encoding='utf-8') print(str(b, encoding='ascii', errors='ignore'))
The result is:
TipsMake
Here, the character ö cannot decode in ASCII. Therefore, it will cause an error. However, we have set the errors = 'ignore' parameter so Python will ignore the non-decode characters with the str () function .
- Python functions built in
- What is Python? Why choose Python?
- The for loop in Python
You should read it
May be interested
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.