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:

  1. Object : An object that can be displayed as a string. If not supplied, the result is an empty string.
  2. encoding : Encoding of an object. If not provided, the default encoding is UTF-8.
  3. errors : Respond when encoding fails. The default value is 'strict'.

There are 6 types of errors:

  1. strict : The default response raises the UnicodeDecodeError exception when an error occurs
  2. ignore : Removes Unicode which cannot be encoded in the result
  3. replace : Replace non-Unicode encoding with a question mark
  4. xmlcharrefreplace : Insert an XML character reference instead of Unicode that cannot be encoded
  5. blackslashreplace : Insert a string uNNNN instead of Unicode which cannot be encoded
  6. nameraplace : Insert a string N {.} instead of Unicode which cannot be encoded
The str () function in Python Picture 1 The str () function in Python returns the string form of an object

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 .

  1. Python functions built in
  2. What is Python? Why choose Python?
  3. The for loop in Python
4 ★ | 1 Vote

May be interested

  • The function set () in PythonThe function set () in Python
    in this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
  • Help () function in PythonHelp () function in Python
    the built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
  • Sum () function in PythonSum () function in Python
    the built-in function sum () in python returns the sum of all numbers in iterable.
  • The slice () function in PythonThe slice () function in Python
    the slice () function in python returns a slice object that helps you determine how to cut an existing string.
  • The chr () function in PythonThe chr () function in Python
    in python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
  • The iter () function in PythonThe iter () function in Python
    continuing 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 in Python
    the 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 in Python
    the reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
  • Exec () function in PythonExec () function in Python
    the 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 functionsBuilt-in Python functions
    in 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.