All data into the program is just a bunch of bytes
English character takes up one byte Chinese is two bytes one byte byte=8bit
Unicode strings set a uniform and unique binary encoding for each character in each language
Big = R ' This is a \tsting ' R denotes the original, what is inside the string is what, the backslash is a backslash, and will not escape
S.isdigit () determines whether a string is a number
Ord and CHR characters and conversions that correspond to the value of the Andean Code
Ord (a)
Chr (97)
Determine if the object is a string:
def isstringlike (anobj):
Try:anobj + "
Except:return False
Else:return True
String alignment:
S.ljust (20)
S.rjust (20)
S.center (20)
S.center (20, ' + ') is not filled with spaces, but with a plus sign
Strip both ends of the string:
X.lstrip ()
X.rstrip ()
X.strip ()
S.strip (' xy ') can also specify what to remove
Python built-in functions:
Filter (fun, seq)
Map (fun, seq)
Reduce (fun, seq) sequentially iterate
Reduce (fun, seq, starting value)
Lambda X, Y:x+y
Operator module
Import operator
Operator.add
..... subtraction, etc...
Help (operator)
Verbatim reversal of strings:
S[::-1]
Reverse the word only:
Import re
Rev = Re.split (R ' (\s+) ', ' Hello World hahaha! ‘)
Rev.reverse ()
Rev = '. Join (REV) #要用空字符来join because the space is already in the cut list
Returns all elements in a that are not part of B:
Set (a). Difference (set (b))
Translate method:
Import string
s = ' Hello world! '
Table = String.maketrans (' abcde ', ' 12345 ') #先生成对照表
S.translate (table) #依照对照表进行替换操作
S.translate (table, ' wor ') #替换后, delete characters containing wor
If you want to delete only, set the table to: Table = String.maketrans (","), which will only do the delete operation.
3/4 #0
From __future__ Import Division
3/4 #0.75
4//4 #0
Capitalization issues:
S.upper () All Caps
S.lower () All lowercase
S.capitalize () First uppercase, remaining lowercase
S.tittle () capitalize the first letter of each word, the remaining lowercase
S.split ()
S.split (' \ n ')
S.splitlines ()
S.splitlines (True) preserves end-of-line newline characters
Python string tricks from Python Cookbook