--- title: Python - String, Bytes and ByteArray tags: python, string --- [TOC] --- # String > Cause string is immutable object, any function changing the string always return a new string object. ## 1. Test Function - `isalnum()` - Return True, if string only contains number or alphabet - `isalpha()` - Return True, if string only contains alphabet - `isdigit()` - Return True, if string only contains number - 只認阿拉伯數字 - `isnumeric()` - Return True, if string only contains number - 不只阿拉伯數字,甚至認得中文 ```python= a = '一二三' a.isnumeric() # True a.isdigit() # False ``` - `isspace()` - Return True, if string only contains whitespace charactor - `isidentifier()` - Return True, if string is python identifier - `islower()` - Return True, if string is lower case - `isupper()` - Return True, if string is upper case - `startswith(s)` - Return True, if string starts with `'s'` - `endswith(s)` - Return True, if string ends with `'s'` ## 2. Search Function - `find(s)` - Return the first found index of `'s'` in the string which starts from index 0 - Return -1 if `'s'` is not in stirng - `rfind(s)` - Return the first found index of `'s'` in the string which starts from last index - Return -1 if `'s'` is not in stirng - `count(s)` - Return how many times `'s'` appers in the string ## 3. Common Function - `lower()` - Return string is lower case - `upper()` - Return string is upper case - `strip(char)` - `rstrip(char)` - `lstrip(char)` - `replace(old, new)` - Return string has replaced old sub_string to new sub_string - EX:`str.replace('\n', '').replace('\r', '').replace('\t', '')` - `split(sep=None, maxsplit=-1)` - Return a list of the words in the string, using sep as the delimiter string. - EX:`'He ll o'.split(' ') --> ['He', 'll', 'o']` - `join(iterable)` - Return a new string that inserted string between the each element in the iterable - EX:`','.join(['He', 'll', 'o']) --> 'He,ll,o'` ## 4. Format Function - format sign - `{:d}`: int - `{:X.Yf}`: float - X is total numbers - Y is the numbers after . - `{:e} {:E}`: scientific sign - `{:x} {:X}`: 0x16 - `{:x} {:X}`: 08 - `{:b}`: binary - simple function - `{!a}`: ascii() - `{!s}`: str() - `{!r}`: repr() - align - `{>}`: right - `{<}`: left - `{^}`: middle - general - `{[arg_name] : [padding] [align] [width]}` - EX: `'{:_>5}'.format('***')` -> `__***` - EX: `'{03d}'.format(51)` -> `051` ### format ```python= n = ['one', 'two', 'three'] # By order '{} {} {}'.format(n[0], n[1], n[2]) # one two three # By number '{2} {1} {0}'.format(n[0], n[1], n[2]) # three two one # By name '{a} {b} {c}'.format(c=n[0], b=n[1], a=n[2]) # three two one ``` ### f"..." ```python= n = ['one', 'two', 'three'] f'{n[0]} {n[1]} {n[2]}' ``` ### Efficiency [參考](https://towardsdatascience.com/do-not-use-to-join-strings-in-python-f89908307273) ```python= # Fast f'{s} {t}' ' '.join((s, t)) '%s %s' % (s, t) '{} {}'.format(s, t) Template('$s $t').substitute(s=s, t=t) # Slow ``` ## 5. Other Functions - `maketrans(x[, y[, z]])` - This static method returns a translation table usable for `str.translate()`. - `x` - If only one argument is supplied, it must be a **dictionary**. - The dictionary should contain a **1-to-1 mapping** from **a single character string to its translation OR a Unicode number** (97 for 'a') to its translation. - `y` - If two arguments are passed, it must be **two strings with equal length**. - Each character in the first string is a replacement to its corresponding index in the second string. - 第一個字串的所有字元會配第二個字串對應位置的字元替換 - `z` - If three arguments are passed, each character in the third argument is **mapped to None**. - `translate(table)` - `table` - Create by `str.maketrans()` ```python= sample = 'abc def xyz' table = str.maketrans( {'a': 'z', 'b': 'y', 'c': 'x'} ) sample.translate(table) # Output: 'zyx def xyz' table2 = str.maketrans('def', 'zoo') sample.translate(table2) # Output: 'abc zoo xyz' table3 = str.maketrans('', '', 'bey') sample.translate(table3) # Output: 'ac df xz' table4 = str.maketrans('abcdefxyz', '123456789', 'bey') sample.translate(table4) # Output: '13 46 79' ``` --- # String Module > import string ## Attribute - `punctuation` ``` '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' ``` --- # Bytes - It is immutable ## Functions - `fromhex(hex_string)` - hex string to bytes ```python= b = bytes.fromhex("0f1c") ``` - `hex()` - bytes to hex string ```python= # Create bytes b = b'\x41\x42' b = bytes( n for n in range(ord('A'), ord('Z')+1) ) b = 'AB'.encode() # default is UTF-8 b = 'AB'.encode('ascii') ``` # ByteArray - It is mutable