*Memos:
- My post explains a string.
str() can create a string with or without many kinds of objects or can decode a bytes-like object as shown below:
*Memos:
- The 1st argument is
object
(Optional-Default:''
orb''
): *Memos:- If
encoding
and/orerrors
are/is set, it must be a bytes-like object to decode it and its default value isb''
, working as decode(). -
b""
,B''
orB""
can be used.
- If
- The 2nd argument is
encoding
(Optional-Default:'utf-8'
): *Memos:-
'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'
): *Memos:- It controls decoding error with the error handlers,
'strict'
,'ignore'
,'replace'
,'xmlcharrefreplace'
,'backslashreplace'
, etc. -
'strict'
raises UnicodeError if the character, which cannot be decoded, exists. -
'ignore'
ignores the character which cannot be decoded. -
'replace'
replaces the character, which cannot be decoded, with�
. -
'xmlcharrefreplace'
replaces the character, which cannot be decoded, with a XML character e.g.ё
. -
'backslashreplace'
replaces the character, which cannot be decoded, with\uxxxx
e.g.\u0451
. - 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,
v = str() # Empty string v = str('12') # String v = str(12) # Integer v = str(1.2) # Floating-point number v = str(1.2+3.4j) # Complex number v = str([1, 2, 3]) # List v = str((1, 2, 3)) # Tuple v = str({1, 2, 3}) # Set v = str({'name':'John'}) # Dictionary v = str(iter([1, 2, 3])) # Iterator v = str(lambda: 10) # Function # No errors
v = str() # Empty string print(v) # Nothing
v = str(object='12') # String print(v, v[0], v[1]) # 12 1 2
v = str(object=12) # Integer print(v, v[0], v[1]) # 12 1 2
v = str(object=1.2) # Floating-point number print(v, v[0], v[1], v[2]) # 1.2 1 . 2
v = str(object=1.2+3.4j) # Complex number 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=[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={'name':'John'}) # Dictionary 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=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=lambda: 10) 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
v = b'H\xd1\x91ll\xcf\x86!' print(str(object=v, encoding='utf-8', errors='strict')) print(str(object=v, encoding='utf-8')) print(str(object=v, errors='strict')) # Hёllφ!
v = b'H+BFE-ll+A8Y!' print(str(object=v, encoding='utf-7')) # Hёllφ!
v = b'\xff\xfeH\x00Q\x04l\x00l\x00\xc6\x03!\x00' print(str(object=v, encoding='utf-16')) # Hёllφ!
v = b'H\xc7\xcell\xa3p!' print(str(object=v, encoding='big5')) # Hёllφ!
import codecs def hashreplace_handler(s): return ((s.end - s.start) * '#', s.end) codecs.register_error('hashreplace', hashreplace_handler) v = b'H\xd1\x91ll\xcf\x86!' print(str(object=v, encoding='ascii', errors='ignore')) # Hll! print(str(object=v, encoding='ascii', errors='replace')) # H��ll��! print(str(object=v, encoding='ascii', errors='hashreplace')) # H##ll##! print(str(object=v, encoding='ascii')) print(str(object=v, encoding='ascii', errors='strict')) # UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 # in position 1: ordinal not in range(128)
v = b'Hёllφ!' print(str(object=v, encoding='ascii', errors='xmlcharrefreplace')) # Hёllφ!
v = b'H\\u0451ll\\u03c6!' print(str(object=v, encoding='ascii', errors='backslashreplace')) # H\u0451ll\u03c6!
Top comments (0)