search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

Python | str constructor

schedule Aug 12, 2023
Last updated
local_offer
Python
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!