Basic Python Tutorial (second edition) Learning Note Strings (Chapter 3rd)
All basic sequence Operations (index, Shard, multiply, Judge membership, seek length, maximum minimum) also apply to strings.
The string is not to be changed;
The left side is the format string, and the right side is the value that needs to be formatted
print '%s=%d '% (' x ', ') ==> x=100
% in percent format string
Template string:
From string import Template
S=template (' $x is 100 '); S.substitute (x= ' ABC '); ==> ' ABC is 100 '
Python's string format is very similar to the C language;
%f floating Point
%s string
%d integers
-%-10.2f left Alignment
+%+5d Positive negative numbers are marked with +-sign
Blank% 5d plus a space before positive numbers
0%010.2f left with 0 padding
Decimal%10.2f 2 decimal places
Minimum width%10.2f width of 10
String.digits Digital
String.letters Letter
String.lowercase Small Letter
string.printable printable characters
String.punctuation punctuation
String.uppercase Capital Letters
String method:
"ABCDEFG". Find ("BC") ==> 1 #查找子串; If a return subscript is found, the return-1 is not found
"ABCDEFG". Find ("DE", 2,7) # provides a range of start and end subscripts for lookups
Join connection string
X=list (' ABC '); "=". Join (x) ==> a=b=c
' ABC '. Lower () #转为小写
' ABCDE '. Replace (' AB ', ' ') ' ==> ' 12CDE ' # replacement substring
' A,b,c '. Split (', ') ==> [' A ', ' B ', ' C '] # If no broken characters are provided, the default is Space, tab, line wrap, and so on.
' ABC '. STRIP () ==> ' ABC ' # Remove the spaces at both ends
'---ABC---! '. Strip ('-! ') ==> ' ABC ' # removes the specified characters from both ends
Translate advanced replacement, you can replace multiple substrings at the same time;
Basic Python Tutorial (second edition) Learning Note Strings (Chapter 3rd)