*Memo:
str() can create a string with or without many types of values or can decode a byte string to a string as shown below:
*Memo:
- The 1st argument is
object
(Optional-Default:''
-Type:Any or Default:b''
-Type:Bytes-like object):- If
encoding
and/orerrors
are/is set, it must be a bytes-like object to decode and its default value isb''
, working as decode().
- If
- The 2nd argument is
encoding
(Optional-Default:'utf-8'
-Type:str
):-
'utf-8'
,'utf-7'
,'utf-16'
,'big5'
,'ascii'
, etc can be set to it. - You can see Standard Encodings for more possible values.
-
- The 3rd argument is
errors
(Optional-Default:'strict'
-Type:str
):- It controls decoding error with the error handlers,
'strict'
,'ignore'
,'replace'
,'xmlcharrefreplace'
,'backslashreplace'
, etc. -
'strict'
raises UnicodeError if the character or byte, which cannot be encoded or decoded, exists. -
'ignore'
ignores the character or byte which cannot be encoded or decoded. -
'replace'
replaces the character or byte, which cannot be encoded or decoded, with?
for encoding or�
for decoding. -
'xmlcharrefreplace'
replaces the character, which cannot be encoded, with the XML/HTML numeric character reference format&#num;
:- It doesn't support decoding so error occurs for the byte which cannot be decoded while error doesn't occur for the byte which can be decoded.
-
'backslashreplace'
replaces the character or byte, which cannot be encoded or decoded, with the hexadecimal format\xhh
,\uxxxx
or\Uxxxxxxxx
for encoding or\xhh
for decoding. - You can see more error handlers.
- You can create your own error handler with codecs.register_error().
- It controls decoding error with the error handlers,
<Create a string>:
v = str() # Empty str
v = str(object='') # Empty str
v = str(object='12') # str
v = str(object=b'12') # bytes
v = str(object=bytearray(b'12')) # bytearray
v = str(object=12) # int
v = str(object=1.2) # float
v = str(object=1.2+3.4j) # complex
v = str(object=True) # bool
v = str(object=[1, 2, 3]) # list
v = str(object=(1, 2, 3)) # tuple
v = str(object={1, 2, 3}) # set
v = str(object=frozenset({1, 2, 3})) # frozenset
v = str(object={'name':'John'}) # dict
v = str(object={'name':'John'}.keys()) # dict.keys()
v = str(object={'name':'John'}.values()) # dict.values()
v = str(object={'name':'John'}.items()) # dict.items()
v = str(object=iter([1, 2, 3])) # iterator
v = str(object=range(1, 4) # range
v = str(object=lambda: 10) # function
# No errors
# Empty str
print(str())
print(str(object=''))
# Nothing
v = str(object='12') # str
print(v, v[0], v[1])
# 12 1 2
v = str(object=b'12') # bytes
print(v, v[0], v[1], v[2], v[3], v[4])
# b'12' b ' 1 2 '
v = str(object=bytearray(b'12')) # bytearray
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# bytearray(b'12') b y t e a r r a y (
v = str(object=12) # int
print(v, v[0], v[1])
# 12 1 2
v = str(object=1.2) # float
print(v, v[0], v[1], v[2])
# 1.2 1 . 2
v = str(object=1.2+3.4j) # complex
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# (1.2+3.4j) ( 1 . 2 + 3 . 4 j )
v = str(object=True) # bool
print(v, v[0], v[1], v[2], v[3])
# True T r u e
v = str(object=[1, 2, 3]) # list
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8])
# [1, 2, 3] [ 1 , 2 , 3 ]
v = str(object=(1, 2, 3)) # tuple
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8])
# (1, 2, 3) ( 1 , 2 , 3 )
v = str(object={1, 2, 3}) # set
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8])
# {1, 2, 3} { 1 , 2 , 3 }
v = str(object=frozenset({1, 2, 3})) # frozenset
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# frozenset({1, 2, 3}) f r o z e n s e t (
v = str(object={'name':'John'}) # dict
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# {'name': 'John'} { ' n a m e ' : '
v = str(object={'name':'John'}.keys()) # dict.keys()
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# dict_keys(['name']) d i c t _ k e y s (
v = str(object={'name':'John'}.values()) # dict.values()
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# dict_values(['John']) d i c t _ v a l u e
v = str(object={'name':'John'}.items()) # dict.items()
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# dict_items([('name', 'John')]) d i c t _ i t e m s
v = str(object=iter([1, 2, 3])) # iterator
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# <list_iterator object at 0x00000200F4715930> < l i s t _ i t e r
v = str(object=range(1, 4)) # range
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# range(1, 4) r a n g e ( 1 , 4
v = str(object=lambda: 10) # function
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
# <function <lambda> at 0x00000200F3EA8E00> < f u n c t i o n
<Decode a byte string to a string>:
print(str(object=b'', encoding='utf-8', errors='strict'))
print(str(encoding='utf-8', errors='strict'))
print(str(encoding='utf-8'))
print(str(errors='strict'))
# Nothing
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='utf-8')
print(b)
# b"L\xd1\x91\xd1\x82's g\xcf\x86!"
print(str(object=b, encoding='utf-8'))
# Lёт's gφ!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='utf-7')
print(b)
# b"L+BFEEQg's g+A8Y!"
print(str(object=b, encoding='utf-7'))
# Lёт's gφ!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='utf-16')
print(b)
# b"\xff\xfeL\x00Q\x04B\x04'\x00s\x00 \x00g\x00\xc6\x03!\x00"
print(str(object=b, encoding='utf-16'))
# Lёт's gφ!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='big5')
print(b)
# b"L\xc7\xce\xc7\xdb's g\xa3p!"
print(str(object=b, encoding='big5'))
# Lёт's gφ! (Let's go!)
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='ignore')
print(b)
# b"L's g!"
print(str(object=b, encoding='ascii', errors='ignore'))
# L's g!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='replace')
print(b)
# b"L??'s g?!"
print(str(object=b, encoding='ascii', errors='replace'))
# L??'s g?!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='xmlcharrefreplace')
print(b)
# b"Lёт's gφ!"
print(str(object=b, encoding='ascii', errors='xmlcharrefreplace'))
# Lёт's gφ!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='backslashreplace')
print(b)
# b"L\\u0451\\u0442's g\\u03c6!"
print(str(object=b, encoding='ascii', errors='backslashreplace'))
# L\u0451\u0442's g\u03c6!
import codecs
def hashreplace_handler(x):
return ((x.end - x.start) * '#', x.end)
codecs.register_error('hashreplace', hashreplace_handler)
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='hashreplace')
print(b)
# b"L##'s g#!"
print(str(object=b, encoding='ascii', errors='hashreplace'))
# L##'s g#!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='strict')
# UnicodeEncodeError: 'ascii' codec can't encode characters
# in position 1-2: ordinal not in range(128)
import codecs
def hashreplace_handler(x):
return ((x.end - x.start) * '#', x.end)
codecs.register_error('hashreplace', hashreplace_handler)
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='utf-8', errors='strict')
print(b)
# b"L\xd1\x91\xd1\x82's g\xcf\x86!"
print(str(object=b, encoding='ascii', errors='ignore'))
# L's g!
print(str(object=b, encoding='ascii', errors='replace'))
# L����'s g��!
print(str(object=b, encoding='ascii', errors='backslashreplace'))
# L\xd1\x91\xd1\x82's g\xcf\x86!
print(str(object=b, encoding='ascii', errors='hashreplace'))
# L####'s g##!
print(str(object=b, encoding='ascii', errors='strict'))
# UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1
# in position 1: ordinal not in range(128)
print(str(object=b, encoding='ascii', errors='xmlcharrefreplace'))
# TypeError: don't know how to handle UnicodeDecodeError in error callback
Top comments (0)