search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Python | str constructor

Built-in Functions
chevron_right
Documentation
schedule Jul 1, 2022
Last updated
local_offer Python
Tags

Python's str(~) constructor returns a string version of the object.

Parameters

1. object | object

The object to convert into a string.

2. encodinglink | encoding type | optional

The encoding of the provided object. Default is utf-8.

3. errorslink | error method | optional

Specifies how to handle errors. Default is strict.

Type

Description

strict

Raise UnicodeError upon encoding failure.

ignore

Ignore the malformed data and continue without further notice.

replace

Replace with a suitable replacement marker for encoding.

xmlcharrefreplace

Replace with the appropriate XML character reference.

backslashreplace

Replace with backslashed escape sequences.

namereplace

Replace with \N{...} escape sequences.

Return value

A string version of the object. If no object is provided, an empty string is returned.

Examples

To return a string version of 15:

a = 15
b = str(a)
15
<class 'str'>

Encoding parameter

To specify encoding of 'utf-8':

# Create a bytes object and store it to variable y
y = bytes('marché', encoding='utf-8')
str(y, encoding='utf-8')
'marché'

The bytes object y which was encoded using utf-8 can successfully be decoded and converted to a string.

Errors parameter

To specify 'replace' error method and decode using 'ascii':

# Create a bytes object and store it to variable z
z = bytes('marché', encoding='utf-8')
str(z, encoding='ascii', errors='replace')
'march��'

It is not possible to decode é in ASCII hence '�' replacement marker is used instead of raising an error.

To decode 'marché' using 'ascii' and 'strict' error method:

# Create a bytes object and store it to variable z
z = bytes('marché', encoding='utf-8')
str(z, encoding='ascii', errors='strict')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5

It is not possible to decode é in ASCII and as we have specified 'strict' mode to handle errors a UnicodeDecodeError is raised.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!