*Memo:
- My post explains a string (1).
- My post explains a string (2).
- My post explains a string (3).
- My post explains a string (4).
- My post explains str() (2).
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
encodingand/orerrorsare/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 and byte, which cannot be encoded and decoded, exists respectively. -
'ignore'ignores the character and byte which cannot be encoded and decoded respectively. -
'replace'replaces the character and byte, which cannot be encoded and decoded, with?for encoding and�for decoding respectively. -
'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 and byte, which cannot be encoded and decoded, with the hexadecimal format\xhh,\uxxxxor\Uxxxxxxxxfor encoding and\xhhfor decoding respectively. - 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 error # 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
Top comments (0)