Menu Close

Python str function

In this article, we are going to learn all about the Python str() function to returns a string object.
If you want to convert any object into a string, Then it will be the best function to convert any object into a string.

Python str function

Python string is a built-in function that is used to convert any object into a string. str function in Python converted 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.

Python str function examples

Here we will take some examples to understand str function in Python.

Example 1:

x = 12.21
print(type(x))
result = str(x)
print(type(result))

Output

<class 'float'>
<class 'str'>

Example 2:

x = [1,2,3,4,5]
for i in x:
	print(str(i))

Output

1
2
3
4
5

Conclusion

In this article, you have learned all about the Python str function to convert any object into a string. In previous tutorials, we have learned all about Python built-in functions along with examples.

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

Python built-in functions


For more information:- Click Here

Previous Article
Next Article

Was this post helpful?

Related Posts