# String elements: single letters, numbers, characters. A single character are called elements.
s = 'hello!'
Length statistics: 1.len (data)
print (len (s)) # 6
2. String Value: String name [index]
# Forward index: from 0 0,1,2 ......
# reverse index: ...... -6, -5, -4, -3, -2, -1
Print (S [. 5]) # index,! Print (S [-1]) # !
# String takes a plurality of values: slices string name [index head: tail Index: step] defaults to step 1
Print (S [. 1:. 5: 2]) # EL # take the first tail does not take l, 3 Print (S [. 1:. 5]) # ello # 1,2,3,4 Print (S [2:. 4: 2 ]) # L # 2 Print (S [:]) # Hello # take all! Print (S [:. 4]) # Hell # 0,1,2,3 taken before all. 3 Print (S [. 3:]) # ! 3 # 3,4,5 LO take all subsequent Print (S [-1: -7: -1]) # reverse output: olleh! Print (S [:: -. 1]) # reverse output:! olleh
3. .split string into string ( 'cut can be specified symbol', designated cutting times) --- string can be used to return a list of data types, the list of sub-elements is a string type
print (s.split ( ' e ' )) # [ 'h', 'pment!'] print (s.split ( ' l ' )) # [ 'I', '', 'o!'] print (s .split ( ' l ' , 1)) # [ 'I', 'it!']
.Replace 4. string replacement string ( 'may specify an alternate value', 'new value', the number of the replacement specified)
s1 = 'heelo!' new = s1.replace('o', '@') print(new) # heel@! new1 = s1.replace('e', '1', 2) print(new1) # h11lo!
The removal of the specified character string string .strip ( 'designated characters removed', the number of the replacement specified)
# Characters only to turn around and tail, can not be removed directly characters
= S ' Hello ' NEW3 = s.strip () # default to space Print (NEW3) # Hello New4 = s.strip ( ' of He ' ) Print (New4) # LLO
6. The splicing string + + ensure both sides correspond to the type of variable values
S_1 = ' Python, ' S_2 = ' available for purchase! ' S_3 = 666 Print (S_1 + S_2 + STR (S_3)) # After S_3 cast to splice # Python, available for purchase! 666 Print (S_1, S_2, S_3) # Python, welcome! 666
7.% format string formatted output
= 18 Age name = ' zhengzi ' Print ( " Welcome to the " + str (Age) + " years old " + name + " of the blog garden " ) # Welcome to the 18-year-old's blog zhengzi Park # (1) formatted output 1: format to use {} placeholder Print ( " Welcome to {{}}-year-old garden of the blog " .format (Age, name)) # Welcome to the 18-year-old zhengzi the blog garden, default order Print ( " Welcome to the {1} blog park, she is 0} { " .format (Age, name)) # Welcome to the zhengzi blog garden, her 18-year- #(2) Output Format 2:%% s string (any data), digital% d (integer)% f float (% .2f rounded to two decimal places) Print ( " Welcome to the age of% d% s the garden blog " % (Age, name)) # Welcome to the 18-year-old's blog zhengzi Park