str() in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

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.

Syntax of str() in Python

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 str() in Python

The method - str() in python takes three parameters in which one of which is required and the other two are optional. The parameters provided to the str() in python are:

  • object: The object parameter is a required parameter. The object is any object or value(s) that is to be converted into a string object. If we do not provide any object in the parameter, the str in python returns an empty string.
  • encoding: The encoding parameters stores the encoding of the provided object. If we do not provide any encoding parameter, the default value i.e. UTF-8 is provided.
  • errors: The error stores the specified action that needs to be performed if the conversion or decoding fails. We can simply say that error is the response when decoding fails.

Return Values of str() in Python

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.

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.

Example of str() in Python

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

Output:

What is 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.

How to use str() in Python?

We can use the str in python to convert an object to a string version of the given object or number. We need to provide the object that needs to be converted into a string. Along with the object, we can also provide the decoding type (like UTF-8) as well as the response took if the decoding fails.

More Examples

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:

:::{.tip} 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:

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. Its syntax is: str(object, encoding = 'utf-8', errors = 'strict').
  • The object parameter is required but the encoding and errors parameters are optional.

Read More: