DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

String in Python (2)

Buy Me a Coffee

*My post explains a string.

str() can create a string with or without many kinds of objects as shown below. *\' is the escape sequence to output ':

*Memos:

  • The 1st argument is object(Optional-Default:'' or b''): *Memos:
    • If encoding and/or errors are/is set, it must be a bytes-like object to decode it and its default value is b''.
    • The uppercase B can also be used e.g B''.
  • 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 encoding error with the error handlers, 'strict', 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace', etc.
    • 'strict' raises UnicodeError if the character, which cannot be encoded, exists.
    • 'ignore' ignores the character which cannot be encoded.
    • 'replace' replaces the character, which cannot be encoded, with e.g. .
    • 'xmlcharrefreplace' replaces the character, which cannot be encoded, with a XML character e.g. ё.
    • 'backslashreplace' replaces the character, which cannot be encoded, with \uxxxx e.g. \u0451.
    • You can see more error handlers.
    • You can create your own error handler with codecs.register_error().
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
Enter fullscreen mode Exit fullscreen mode
v = str() # Empty string

print(v) # Nothing
Enter fullscreen mode Exit fullscreen mode
v = str(object='12') # String

print(v, v[0], v[1])
# 12 1 2
Enter fullscreen mode Exit fullscreen mode
v = str(object=12) # Integer

print(v, v[0], v[1])
# 12 1 2
Enter fullscreen mode Exit fullscreen mode
v = str(object=1.2) # Floating-point number

print(v, v[0], v[1], v[2])
# 1.2 1 . 2
Enter fullscreen mode Exit fullscreen mode
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 )
Enter fullscreen mode Exit fullscreen mode
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 ]
Enter fullscreen mode Exit fullscreen mode
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 )
Enter fullscreen mode Exit fullscreen mode
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 }
Enter fullscreen mode Exit fullscreen mode
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 ' :   '
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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  
Enter fullscreen mode Exit fullscreen mode
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φ!
Enter fullscreen mode Exit fullscreen mode
v = b'H+BFE-ll+A8Y!'

print(str(object=v, encoding='utf-7'))
# Hёllφ!
Enter fullscreen mode Exit fullscreen mode
v = b'\xff\xfeH\x00Q\x04l\x00l\x00\xc6\x03!\x00'

print(str(object=v, encoding='utf-16'))
# Hёllφ!
Enter fullscreen mode Exit fullscreen mode
v = b'H\xc7\xcell\xa3p!'

print(str(object=v, encoding='big5'))
# Hёllφ!
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode
v = b'H&#1105;ll&#966;!'

print(str(object=v, encoding='ascii', errors='xmlcharrefreplace'))
# H&#1105;ll&#966;!
Enter fullscreen mode Exit fullscreen mode
v = b'H\\u0451ll\\u03c6!'

print(str(object=v, encoding='ascii', errors='backslashreplace'))
# H\u0451ll\u03c6!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)