w3resource

Python: str() function

str() function

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

Version

(Python 3.2.5)

Syntax:

str(object='')
str(object=b'', encoding='utf-8', errors='strict')

Parameter:

Name Description
object Any object.
encoding

The encoding of the given object. Default is UTF-8.

errors

Specifies what to do if the decoding fails.

Return value:

A string.

Example: Python str() function

print(str(15))

Output:

15

Example: Python str() function

# bytes
x = bytes('cöde', encoding='utf-8')

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

Output:

cde

Pictorial Presentation:

Python: Built-in-function - str() function

Python Code Editor:

Previous: sorted()
Next: sum()

Test your Python skills with w3resource's quiz



Python: Tips of the Day

How do I check if a list is empty?

For example, if passed the following:

a = []
if not a:
  print("List is empty")

Ref: https://bit.ly/2A4JXx9