Methods and Functions

Python str()

The Python Programming language supports multiple built-in functions. These functions are predefined and were created to perform a specific task or task. Python also provides certain built-in functions using which we can convert values of one type of data to another type of data. Let us learn str python here in detail.

str python 

The str()

The str() is one such predefined built-in function that is used to convert the given value into its equivalent string type. The syntax of the str() is as follows (one must make sure to note down the correct syntax of the function to avoid any possible errors): 

str(object, encoding=encoding, errors=errors)

The str Python is useful when you would like to concatenate integer or float type values to the string. 

str() Parameters

According to the syntax given above, the string function has three parameters:

  1. The Object Parameter- The object can be of any datatype. This parameter specifies the object that must be converted into the string data type. If there is no data fed into the object parameter, it returns an empty string.  
  2. Encoding Parameter- This defines the encoding of the object. The default value of the encoding parameter is UTF-8.
  3. Errors Parameter- This parameter specifies what must be done in case the decoding fails. The default value of the error parameter is ‘strict’. The value of the errors parameter can be changed by entering a different errors value.

Types of Errors Values

The str Python accepts 6 different types of error values. These are given below for our reference:

  1. strict- This is the default errors value. If the errors parameter has a strict value, then in case of any errors the UnicodeDecodeError exception will be raised.
  2. Replace- In this case, the code that cannot be decoded will be replaced by a ? by the str().
  3. ignore- If the errors value is set to ignore, the undecoded character will be ignored from the str result. 
  4. xmlcharrefreplace- The undecoded characters, in this case, will be returned with an XML character reference.
  5. backslashreplace The undecoded characters, in this case, will be returned with an \uNNNN escape sequence.
  6. name replace  The undecoded characters, in this case, will be returned with a \N{…} escape sequence

Return value from str()

The str() returns an object of the string datatype. Here are some examples that will help you understand the str() much better:

Example 1: Convert to String


num = 100
s = str(num)
print (s)

In this example, we are converting the ‘num’ integer into a string object using the str(). Here, the values of the encoding and the errors parameter are not provided.

Example 2: How str() works for bytes?


a = bytes('Americàn', encoding='utf-8')

print(str(a, encoding='ascii', errors='ignore'))



In the example given above, if we provide values to the encoding and the parameters of the error, then we must make sure that the data type of the object parameter must either be in bytes or a byte array, i.e., it should be a bytes-like-object. 

From the sample code given above, the à character cannot be identified by the ASCII. In this case, the str Python should have returned an error instead it gave ‘Americn’ as output. This is because the parameter value of errors was set as errors =’ ignore’. Hence the compiler ignores the characters that cannot be decoded by the str(). 

Similarly, if the errors value was set as ‘replace’ then the output would have been:


a = bytes('Americàn', encoding='utf-8')

print(str(a, encoding='ascii', errors='replace'))



FAQs on str python

1. What does str () do in Python?

Ans: str() is a built-in function in the Python programming language that is used to convert the specified value into a string datatype. For example:

Let us say that we have declared an integer a that has a value of 10.



print(a, type)


10 <class 'int'>

Now we are converting this integer into a string using the str() and storing in it another variable ‘z’


z= str(a)

print (z, type)


10 <class 'str'>

2. What is type STR Python?

Ans: Python has another built-in function named type(). These functions help you find the class of the given variable inside your program. 

For example, type str Python  

Will give the output as <class ‘str’>.

3. Is str a keyword in Python?

Keywords are reserved words that convey a specific meaning. The Python Programming language has 33 keywords. We cannot use these keywords as variable names, identifiers, or function names. 

str is a built-in function in Python. Built-in functions are not keywords., this is because the built-in functions have no restrictions and are always available, unlike keywords that have restrictive usage. 

4. What is STR and INT in Python?

Ans: STR and INT are built-in functions. These functions were created to perform a specific task. 

The int() converts the specified value into an integer type.


int(value, base)

The str() is used to convert the specified value into a string type. 


str(object, encoding=encoding, errors=errors).

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.