string Manipulation
A. String formatted output
1 " Liu " 2 Print " " % name3 4# output: I am Liu 6 PS: string is%s; integer%d; floating-point number%f
B. Finding a string (find)
1 # detects if STR is included in MyStr, or returns 1 if the index value is returned .
1 ' Xinge is so handsome. ' 2 3 >>> str.find ('xing')40 5 6 >>> str.find (' good ')7 6
C. Finding the contents of a replacement string (replace)
1 mystr.replace (str1, str2, Mystr.count (str1)) # replace mystr in str1 with str2 , if Count is specified, replace no more than count Times.
1 ' ABCABCABC ' 2 3 >>> str.replace ('a','xinge' , 2)4'xingebcxingebcabc'
d. Slicing with Str (split)
Mystr.split (str=""# with Str as the delimiter slice mystr, if the maxsplit has a specified value, only the Maxsplit substring is delimited
1>>> str ='A\NB\TC'2 3>>>Str4 'A\NB\TC'5 6>>> Str.Split()7['a','b','C']
F. Capitalize the first letter of the string (capitalize)
1 >>> str='abc'2 3 >>> Str.capitalize ()4'Abc'
G. Capitalize the first letter of each word of a string (title)
1 " Hello World " 2 3 >>> str.title ()4'HelloWorld'
H.startswith,endswith
1 startswith # checks if the string starts with STR, returns TRUE, otherwise False2 endswith # checks whether the string ends with STR, or returns False if True.
I.upper,lower
1 Upper # convert all characters in mystr to uppercase 2 Lower # convert all characters in mystr to lowercase
J.strip,lstrip,rstrip
1 Strip # clears the left and right spaces 2 lstrip # clears the left space 3 Rstrip # Clear the right space
K.join
1 mystr.join (str) # mystr Insert Str After each character, constructing a new string
Welcome to add!
[Python Study Notes] string manipulation