Full stack growth - data type of python study notes - string

strings in python

String Common Methods
method name effect
capitalize() The first letter is capitalized and other letters are lowercased (only valid for strings starting with a letter)
lower() String letters are converted to lowercase (affects letters anywhere in the string)
upper() String letters are converted to uppercase (affects letters anywhere in the string)
swapcase() Uppercase to lowercase Lowercase to uppercase (valid for all alphabetic characters in the string)
zfill(x) Define the length (x) for the string If the missing position is not satisfied, fill it with 0 (0 is added in front of the string)
startswith(val) Determine whether the beginning is val and return a Boolean value
endswith(val) Determine whether the end is val and return a Boolean value
find(val) Returns the index of val in the current string or -1 if the current string does not contain val
index(val) Returns the index of val in the current string. If the current string does not contain val, the program will directly report an error
strip(val) Strip all vals at the beginning and end of the string (spaces by default) aabbaa.strip("a")=bb
lstrip(val) Remove all val at the beginning of the string (the default is a space) aabbaa.strip("a")=bbaa
rstrip(val) Remove all val at the end of the string (default is space) aabbaa.strip("a")=aabb
replace(old,new,maxNu) old old (before replacement) new (after replacement) maxNu (the number that needs to be replaced is not filled in, the default is all)
isspace() Determine whether the string consists of only spaces and return a Boolean value" ".isspace=>true '''.isspace=>false
istitle() Determine whether it is the title "Hellow Word" "Hellow"=>true "Hellow Word"="false"
isupper() Judging whether all letters in the string are all uppercase can have Chinese
islower() Determine whether all letters in the string are all lowercase and can have Chinese characters
String formatting
format method usage
%s ''I am %s age %s' % ("wsm",25)
format() ''I am {0} age {1}'.formar(“wsm”,25)
f f"I am {variable name} age {variable name}" The variable name needs to be defined in advance
Common format characters for string formatting
conform to illustrate
%s format string general type
%d format integer
%f Format floating-point type (6 decimal places after formatting, 0 will be added when it is not enough)
%u Format unsigned integers (positive integers)
%c formatting characters
String Escapes and Escape Nullifiers
escape symbol illustrate
\n A newline is usually at the end
\t horizontal tab (a spacer)
\a After printing, the computer will beep
\b Backspace moves the cursor forward overwriting
\r carriage return
\’ Escaping single quotes for strings
\” Escaping double quotes for strings
\ escape slash
r Putting it at the beginning of the string will invalidate all escape characters in the string

Guess you like

Origin blog.csdn.net/qq_51075057/article/details/130378379