fs = "first" "second"
def p(st):
print(st)
p(
"long_string_1_"
"long_string_2_"
"long_string_3_"
) # "long_string_1_long_string_2_long_string_3_"
"""
\newline Backslash and newline ignored
\\ Backslash (\)
\' Single quote (')
\" Double quote (")
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v SCII Vertical Tab (VT)
\ooo Character with octal value ooo
\x42 Character with hex value instead of 42
\u0086 Character by (2 bytes) unicode id
\U00000086 Character by (4 bytes) unicode id
"""
raw_string = r"\n\r\\\\"
print(raw_string) # '\n\r\\\\'
U+FEFF - BOM-marker
TO understand read bytes <<< or >>> used this maker in the beginning of line
FEFF means >>>
FFEF means <<<
python (is smart) understand encode by itself
ord.__doc__
'Return the Unicode code point for a one-character string.'
list(map(ord, " ")) # [32]
list(map(ord, "hello")) # [104, 101, 108, 108, 111]
list(map(ord, "привет")) # [1087, 1088, 1080, 1074, 1077, 1090]
list(map(ord, "👍")) # [128077]
print(
"\u0068",
"\U00000068"
) # h h
print(
"\N{DOMINO TILE HORIZONTAL-00-00}"
) # 🀱
chr(0x68) # 'h'
chr(1087) # 'п'
"foo bar".capitalize() # 'Foo bar'
"foo bar".title() # 'Foo Bar'
"foo bar".upper() # 'FOO BAR'
"foo bar".lower() # 'foo bar'
"foo bar".title().swapcase() # 'fOO bAR' !!!!!!
"v1a0".ljust(16, '=')
# 'v1a0============'
"v1a0".rjust(16, '=')
# '============v1a0'
"v1a0".center(16, "=")
# '======v1a0======'
"v1a0".center(16)
# ' v1a0 '
"|>>>0<<<|".lstrip("|>")
# '0<<<|'
"|>>>0<<<|".rstrip("<|")
# '|>>>0'
"|>>>0<<<|".strip("|>")
# '0<<<'
"|>>>0<<<|".strip("<|")
# '>>>0'
"|>>>0<<<|".strip("<|>")
# '0'
"\n\n\t 123 ".strip()
# '123'
"hi,bye".split(',') # ['hi', 'bye']
"hi,,,,bye".split(',') # ['hi', '', '', '', 'bye']
" hi bye ".split() # ['hi', 'bye']
# ============================
"filename.tar.gz".split('.')
# ['filename', 'tar', 'gz']
"filename.tar.gz".split('.', 1)
# ['filename', 'tar.gz']
"filename.tar.gz".rsplit('.', 1)
# ['filename.tar', 'gz']
"filename.tar.gz".partition('.')
# ('filename', '.', 'tar.gz')
"filename.tar.gz".rpartition('.')
# ('filename.tar', '.', 'gz')
# It has static structure
# (before_smt, smt, after_smt)
# !!!!!!!!!!!!
"filename_no_dots".rpartition('.')
# ('', '', 'filename_no_dots')
', '.join(['one', 'tow', '3'])
# 'one, tow, 3'
', '.join(filter(None, ["", 'tow', '']))
# 'tow'
', '.join("abcdef")
# 'a, b, c, d, e, f'
"foobar".startswith('foo')
# True
"foobar".endswith('bar')
# True
"foobar".endswith(('foo', 'bar'))
# True
"asdefg".find('de')
# 2
"asdefg".find('de', 0, 3) # == [0:3].find(...)
# -1
"hello-my-name-is-Alex".replace('-', '_')
# 'hello_my_name_is_Alex'
"hello-my-name-is-Alex".replace('-', '_', 2)
# 'hello_my_name-is-Alex'
# multiple replace
trans_map = {ord('a'): 'AA', ord('b'): "??"}
"baobab".translate(trans_map)
# '??AAo??AA??'
# ===========================
# Caesar Cipher in 2 lines by v1a0
rot3 = dict(zip(map(ord, 'abcdefghijklmnopqrstuvwxyz'), 'defghijklmnopqrstuvwxyzabc'))
'tiberivs clavdivs caesar'.translate(rot3)
'1337'.isdigit() # True
'foobar'.isdigit() # False
'1337'.isalnum() # True
'foobar'.isalnum() # True
'1337'.isalpha() # False
'foobar'.isalpha() # True
' \t\n '.isspace() # True
"foo bar".islower()
"FOO BAR".isupper()
"Foo Bar".istitle()
str("123 ф") # '123 ф'
repr("123 ф") # "'123 ф'"
ascii("123 ф") # "'123 \\u0444'"
import string
print(string.ascii_letters)
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
print(string.digits)
# 0123456789
print(string.punctuation)
# !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
print(repr(string.whitespace))
# ' \t\n\r\x0b\x0c'
b1 = b"\00\42\24\00"
# b'\x00"\x14\x00'
rb1 = rb"\00\42\24\00"
# rb"\00\42\24\00"
b"foo bar".title()
# b'Foo Bar'
open(name="myfile.txt", mode=mode, encoding="UTF-8", errors='ignore')
# mode might be
# ('r', 'w', 'x', 'a', '+', 'b', 't')
open("README.md", 'rt').read(16)
# '# Computer Scien'
open("README.md", 'rb').read(16)
# b'# Computer Scien'
open("README.md", 'at').writelines(['hi\n', 'there\n'])
handle = open("README.md", "r+")
handle.fileno() # returns file descriptor number (1:07:00)
# 3
handle.tell() # position in file
# 0
handle.seek(8) # set position in reading file
handle.tell()
# 8
handle.write('something unimportant') # bush changes from buffer to disk
handle.flush() # drop buffer
handle.close() # close file (and drops buffer
print('__any_output__', file=open('README.md', 'a'), flush=True)
print(*[1,2,3], sep="_", end='++')
# 1_2_3++