Python intercepts strings using the variable [head subscript:tail subscript], you can intercept the corresponding string, where the subscripts are counted from 0 and can be positive or negative, and the subscripts can be null to indicate that they are taken to the head or tail.
# Example 1: String Interception
str = '12345678'
print str[0:1]
>> 1 # Output the characters from position 0 to position 1 of str.
print str[1:6]
>> 23456 # Output characters from str position 1 to position 6.
num = 18
str = '0000' + str(num) # merge strings
print str[-5:] # Output the right 5 bits of the string
>> 00018
Python replace string using variable.replace("replaced content", "replaced content" [, times]), the replacement times can be null, that is, means replace all. It is important to note that the use of replace to replace the string is only a temporary variable, you need to re-assign the value to save.
# Example 2: String Replacement
str = 'akakak'
str = ('k',' 8') # Replace all k's in the string with 8's
print str
>> 'a8a8a8' # Output result
Python find string using variable.find("what to find" [, start position, end position]), start position and end position, indicates the range to be found, null means find all. Find will return to the position, the position from 0 to start counting, if every find then return -1.
# Example 3: String Lookup
str = 'a,hello'
print ('hello') # Find the string hello in the string str
>> 2 # Output results
Python splits strings using variable.split("split marker"[split count]), split count indicates the maximum number of splits, null splits all.
Example 4: Character Segmentation
str = 'a,b,c,d'
strlist = (',') # split str string with comma and save to list
for value in strlist: # Loop over the values of the list.
print value
>> a # Output results
>> b
>> c
>> d