Python str Function

This guide deals with the Python str function and it’s uses, complete with several examples.

The str function is used to create string representations of objects. It does not alter the object itself. It simply returns a new string object. The string function comes with it’s own encoding and error handling scheme to aid it’s conversion process.

Syntax

Below is the str function syntax, with it’s 2nd and 3rd parameter’s default values.

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

The str()function can take up-to three parameters:

  • object – The object of which the string representation is required. If no object is passed, str() returns an empty string.
  • encoding – The encoding in which to encode the string. Default value is UTF-8.
  • errors – The error handling scheme to use for encoding errors. Defaults to 'strict' which raises an exception with the UnicodeEncodeError.

List of possible error settings:

  1. strict: Default value of the str function. Raises a UnicodeEncodeError if string failed to convert.
  2. ignore: Ignores any errors during the encoding process
  3. replace: If the encoding fails, it will be replaced with a question mark.
  4. xmlcharrefreplace – Inserts XML character reference in THE place of the un-encodable Unicode object.

For the most part, you don’t need to concern yourself with the encode and errors parameter. Chances are, you won’t ever end up using them. Knowledge never hurts though.

Any object with the __str__ method can be converted to a string.


Example 1:

Keep in mind, that the contents of the list have not turned into strings. The whole list has become a string.

>>> str(5)
'5'

>>> X = [5,6,7]
>>> str(X)
'[5, 6, 7]'

Example 2:

Here we will demonstrating the use of a few extra parameters of the str function. To understand this, you should understood what ASCII is. ASCII is a character encoding set that was created to handle characters. However, ASCII does not include any non-English characters. Hence, Unicode was developed which encompassed ASCII and has support for every language.

How is this relevant? If you encode a non-English character with ASCII, you’ll run into an error. This is shown in the example below, where we use the various settings to show their effect on the output.

# bytes
x = bytes('pythön', encoding='utf-8')

print(str(x, encoding='utf-8', errors='ignore'))
print(str(x, encoding='ascii', errors='ignore'))
print(str(x, encoding='ascii', errors='replace'))
print(str(x, encoding='ascii', errors='strict'))
pythön
pythn
pyth��n
Traceback (most recent call last):
  File "C:\Users\Shado\Desktop\DIEEE.py", line 8, in <module>
    print(str(x, encoding='ascii', errors='strict'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)

This marks the end of the Python str Function Article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be asked in the comments section below.

Here’s a link back to the main Python Built-in Functions page.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments