Skip to content

Latest commit

 

History

History
355 lines (233 loc) · 5.28 KB

File metadata and controls

355 lines (233 loc) · 5.28 KB

Strings, bytes, files, I/O

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 strings

raw_string = r"\n\r\\\\"
print(raw_string)   # '\n\r\\\\'

Bytes to char

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]

All unicode chars have number and name

print(
    "\u0068",
    "\U00000068"
)   # h h

print(
    "\N{DOMINO TILE HORIZONTAL-00-00}"
)   # 🀱

chr(0x68)   # 'h'

chr(1087)   # 'п'

Methods for strings

"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'  !!!!!!

Justify text

"v1a0".ljust(16, '=')
# 'v1a0============'

"v1a0".rjust(16, '=')
# '============v1a0'

"v1a0".center(16, "=")
# '======v1a0======'

"v1a0".center(16)
# '      v1a0      '

xstrip - removes some symbols

"|>>>0<<<|".lstrip("|>")
# '0<<<|'

"|>>>0<<<|".rstrip("<|")
# '|>>>0'

"|>>>0<<<|".strip("|>")
# '0<<<'

"|>>>0<<<|".strip("<|")
# '>>>0'

"|>>>0<<<|".strip("<|>")
# '0'

"\n\n\t   123    ".strip()
# '123'

split

"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']

partition !!!!

"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

', '.join(['one', 'tow', '3'])
# 'one, tow, 3'

', '.join(filter(None, ["", 'tow', '']))
# 'tow'

', '.join("abcdef")
# 'a, b, c, d, e, f'

other

"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

replace

"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)

useful predicates

'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()

convert

str("123 ф")    # '123 ф'
repr("123 ф")   # "'123 ф'"
ascii("123 ф")  # "'123 \\u0444'"

import string

import string

print(string.ascii_letters)
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

print(string.digits)
# 0123456789

print(string.punctuation)
# !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

print(repr(string.whitespace))
# ' \t\n\r\x0b\x0c'

Bytes

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

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'])

File descriptor and other

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

print('__any_output__', file=open('README.md', 'a'), flush=True)

print(*[1,2,3], sep="_", end='++')
# 1_2_3++