String or string (String) It's numbers 、 Letter 、 A string of characters made up of underscores . stay Python in , The string is enclosed in single quotation marks or double quotation marks .
Catalog
String splicing
String processing (8 Common use )
Put together a comma , Then a space will appear in the comma .
Use the plus sign to splice , There are no spaces . If you want spaces , Then a space should be added
print("Hello","World") # Output :Hello World
print("Hello"+"World") # Output :HelloWorld
print("Hello"+" "+"World") # Output :Hello World
len():
Returns the length of the string , That is, the number of single elements in the string
length=len(target_string) #target-string Is the target string variable
upper():
Convert all characters in the string to uppercase
lower():
Convert all characters to lowercase
title():
Change the first letter of all words in the string to uppercase , The other letters are still lowercase
new_string="Tom is good"
print(name_string.upper()) # Output :TOM IS GOOD
print(name_string.lower()) # Output :tom is good
print(name_string.title()) # Output :Tom Is Good
strip()
: You can remove both sides of the string ( Does not include internal ) All spaces . Using this method , You can also specify parameters , Remove specific characters specified on both sides
split()
: Implement string splitting . This method is based on the provided delimiter , Split a string into a list of characters , If no delimiter is provided , The program will default to the space ( Tabulation 、 Line break, etc ) As a separator .
find()
: Using this method, you can find substrings in a long string . If in this string , There are one or more substrings , The method returns the leftmost index of the first substring , If no qualified substring is found , Then return to -1
.
replace()
: To replace substrings in a given string .
source_string.replace(old_string, new_string)