The str function

The str function creates a new string from the original object. In the first parameter, we specify the object from which we will make a string. In the second optional parameter, you can specify the encoding (by default, UTF-8), and in the third optional parameter, the error type in case of failure to convert to a string.

Syntax

pythonstr(the object from which we make a string, [encoding type], [error type])

Example

Let's use the str function to make a string from a number:

pythonnum = 1234
txt = str(num)
print(txt)

Result of code execution:

python'1234'

See also

  • function int,
    which creates the number
  • function float,
    which creates a floating point number
  • function list,
    which creates a list