Menu Close

Python str() function

Python str function

In this article, you will learn everything about the Python str() function with the help of the examples. It is a built-in function that comes with Python by default. It takes an object as a parameter to convert into a string object.

Python str() function Introduction

Python str() function is a built-in function which means you don’t need to install it by using the pip command because it comes with Python by Default. The str() function in Python is used to convert any object into a string. It is also capable of converting bytes or bytearray type of string to string.

Syntax:

The syntax of the str() function in Python.

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

Parameters

str function in Python accepts three parameters.

  • object:- Required. Object to be converted into a string.
  • encoding:- The encoding of the object.
  • errors:- Specifies what to do if the decoding fails.

Note:- encoding and errors parameters are only used when the type of the object is bytes and bytearray.

Types of errors in str() function

There are six types of errors taken by the str() function as errors.

  • strict:- It raise UnicodeDecodeError. By default, It is set.
  • ignore:- It ignores the Unencodable Unicode.
  • replace:- It replaces Unencodable Unicode with Question Marks.
  • xmlcharrefreplace:- It inserts XML character reference instead of Unencodable Unicode.
  • backslashreplace:- It inserts \uNNNN Escape sequence instead of Unencodable Unicode.
  • namereplace:- It inserts \N Escape sequence instead of Unencodable Unicode.

Python str() function examples

Here we will take some examples to see how we can convert float, integer, list, tuple, and Dictionary to string using the Python str() function.

Example: Converting float to string

In this example, we are converting float object x to string.


x = 12.21
print("Type of the x:- ", type(x))
result = str(x)
print("Converted string is:- ", result)
print("Type of result is:- ", type(result))

Output

Type of the x:-  <class 'float'>
Converted string is:-  12.21
Type of result is:-  <class 'str'>

Example: Converting Integer to string

In this example, converting integer x to a string by using the str() function.


x = 100
print("Type of the x:- ", type(x))
result = str(x)
print("Converted string is:- ", result)
print("Type of result is:- ", type(result))

Output

Type of the x:-  <class 'int'>
Convert string is:-  100
Type of result is:-  <class 'str'>

Example: Converting a list to a string

Converting Python list x to string with the help of the str() function.


x = ["Vishvajit", "Pankaj", "Harshita"]
print("Type of the x:- ", type(x))
result = str(x)
print("Converted string is:- ", result)
print("Type of result is:- ", type(result))

Output

Type of the x:-  <class 'list'>
Converted string is:-  ['Vishvajit', 'Pankaj', 'Harshita']
Type of result is:-  <class 'str'>

Example: Converting a tuple to a string

Converting Python tuple to string.


x = (10, 20, 30, 40, 50, 60)
print("Type of tuple is:- ", type(x))
result = str(x)
print("Value after converted tuple to string:- ", result)
print("Type of result is:- ", type(result))

Output

Type of tuple is:-  <class 'tuple'>
Value after converted tuple to string:-  (10, 20, 30, 40, 50, 60)
Type of result is:-  <class 'str'>

Example: Convert Dictionary to string

Converting Python dictionary x to string.


x = {"name": "Vishvajit", "age": 24, "email": "[email protected]", "address": "Noida"}
print("Type of Dictionary is:- ", type(x))
result = str(x)
print("Value after converted Dictionary to string:- ", result)
print("Type of result is:- ", type(result))

Output

Type of Dictionary is:-  <class 'dict'>
Value after converted Dictionary to string:-  {'name': 'Vishvajit', 'age': 24, 'email': '[email protected]', 'address': 'Noida'}
Type of result is:-  <class 'str'>

Example: Converting Bytes string to String

In this example, we have converted the string into bytes by using the Python bytes() function and after that converted it to the string.


string = "Programming Funda"
print("Original String:- ", string)
bytes_string = bytes(string, 'utf-8')
print("Bytes String is:- ", bytes_string)

print("Converting bytes to string:- ", str(bytes_string, encoding="ascii", errors='strict'))

Output

Original String:-  Programming Funda
Bytes String is:-  b'Programming Funda'
Converting bytes to string:-  Programming Funda

Example: Converting Bytes string to String with ignore errors


byte_string = bytes("ŽString", encoding="utf-8")
print("Byte String:- ", byte_string)
print("Converting bytes to string:- ", str(byte_string, encoding="ascii", errors='ignore'))

Output

Byte String:-  b'\xc5\xbdString'
Converting bytes to string:-  String

In the above example, As you can see, we have created a byte object byte_string with string ŽString and the encoding utf-8. We have passed byte_string to the str() function along with encoding ASCII and we have set the errors parameter to ignore. The str() function ignores the character Ž. Since the method can’t decode the character Ž to ASCII and return String as result.

Example: Converting Bytes string to String with strict errors


byte_string = bytes("ŽString", encoding="utf-8")
print("Converting bytes to string:- ", str(byte_string, encoding="ascii", errors='strict'))

Output

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128)

Similarly, In tIn the above example, we have set the errors to strict. In this case str() function takes Ž characters and tries to convert them into ASCII and if it is not possible it produces UnicodeDecodeError.

Conclusion

In this article, you have seen everything about the Python str() function to convert any object into a string and also you have seen how to convert sny bytes type of object to string with the help of encoding and errors parameters. You can explore more Python built-in function from here.

If you like this article, please share and keep visiting for further Python tutorials.

Python built-in functions


For more information:- Click Here

Python slice() function
Python classmethod() function

Related Posts