str() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

To convert a specified value or an object into a string object, we use str in python. The conversion of one data type into the other data type is known as type conversion or type casting in python.

Example of str() in Python

Let us convert a number into a string object using the method, str in python.

Output:

Syntax of Python str() Function

The syntax of the method, str in python is very simple:

The encoding and errors parameters are optional parameters but the object parameter is required.

Parameters of Python str() Function

The method - str in python takes three parameters. The parameters provided to the str() in python are:

  • object: The object parameter is a required parameter.
  • encoding: The encoding parameters stores the encoding of the provided object.
  • errors: It stores the specified action that needs to be performed if the conversion or decoding fails.

Return Values of Python str() Function

The str in python returns the string object value (string version) of the given object or number. If we do not give any parameter in the method, it will return an empty string.

More Examples of Python str() Function

Let us take a few examples to understand the str method in python in a better way.

Example 1: Convert to String

Let us take a number and convert it into the string using str in python:

Output:

Note:

If we do not provide the error and encoding parameter, the str() method internally calls the __str__() method of the object. In some cases, if the __str__() method is not found, the str() method invokes repr(object).

Example 2: How str() works for bytes?

Whenever we provide the error and encoding parameter, the object parameter should be of the type: bytes-like-object or bytes or bytes-array.

Let us take an example where we specify the error and encoding parameter to understand the working of str in python in a better way.

Output:

Let us provide a different encoding standard.

Output:

Understanding str() in Python

As we know, the conversion of one data type into the other data type is known as type casting or type conversion. To convert a specified value or an object into a string object, we use str in python.

What is str in python

The str() in python takes three parameters in which one of them is required and the other two are optional. The object parameter is a required parameter. The object is any object or value(s) that is to be converted into a string object. The encoding parameters stores the encoding of the provided object. The error stores the specified action that needs to be performed if the conversion or decoding fails. The str in python returns the string version of the given object or number.

Exceptions of str() in Python

The str in python does not raise an exception in general. But whenever we do not pass any parameter to the str() function, the str function returns an empty string.

There are 6 types of errors that can be provided as a parameter in the str() method.

  • strict: It is the default value for the error parameter, the strict raises a UnicodeDecodeError.
  • ignore: We can specify ignore to ignore the unencoded Unicode.
  • replace: The replace is used to replace the un-encodable Unicode with a question mark (?).
  • xmlcharrefreplace: The xmlcharrefreplace is used to insert XML characters in place of the un-encodable Unicode.
  • backslashreplace: The backslashreplace is used to insert escape sequences (\uNNNN) in place of un-encodable Unicode.
  • namereplace: The namereplace is used to insert \N{....} escape sequence in place of un-encodable Unicode.

Conclusion

  • To convert a specified value or any object into a string object, we use str in python.
  • The conversion of one data type into the other data type is known as type casting or type conversion.
  • The str in python takes three parameters and returns an equivalent string object.
  • The object parameter is required but the encoding and errors parameters are optional.

See Also