Python intercepts the string using the variable [header subscript: tail subscript], you can intercept the corresponding string, where the subscript is starting from 0, can be positive or negative, subscript can be empty to take the head or tail.
Copy the Code code as follows:
# example 1: string interception
str = ' 12345678 '
Print Str[0:1]
>> 1 # Output str position 0 start to position 1 characters
Print Str[1:6]
>> 23456 # output str position 1 start to position 6 characters
num = 18
str = ' 0000 ' + str (num) # Merging strings
Print str[-5:] # Output string Right 5 bits
>> 00018
The Python substitution string uses variables. replace ("replaced content", "replaced content" [, number of times]), and the number of replacements can be null, which means replace all. Note that replacing a string with replace is only a temporary variable and needs to be re-assigned to save.
Copy the Code code as follows:
# example 2: String substitution
str = ' Akakak '
str = str.replace (' k ', ' 8 ') # Replace all k in the string with 8
Print str
>> ' a8a8a8 ' # output results
The Python lookup string uses variables. FIND ("What to Look for" [, start position, end position]), start and end position, indicate the range to look for, or null to find all. The location is returned after finding it, and the position is calculated from 0, and returns 1 if each is found.
Copy the Code code as follows:
# example 3: String lookup
str = ' A,hello '
Print str.find (' hello ') # Find string in string str Hello
>> 2 # Output results
The Python split string uses a variable. Split ("split symbol" [Number of splits]), the number of splits indicates the maximum number of times, and the empty is split all.
Example 4: Character segmentation
Copy the Code code as follows:
str = ' A,b,c,d '
Strlist = Str.split (', ') # splits the STR string with a comma and saves it to the list
For value in strlist: # Loop Output List value
Print value
>> A # Output results
>> b
>> C
>> D