Interception of strings in Python string operations

String interception

String interception is a technique often used in practical applications, and the intercepted part is called a "substring". In Java, the function substr() is used to obtain a substring, and in C#, the function substring() is used to obtain a substring. Since Python has built-in sequences, you can obtain substrings through the indexing and slicing introduced earlier, or you can use the function split() to obtain them. Strings are also sequences.

The following code uses the index of the sequence to get a substring:

 

# Use index to intercept substring

print('----------# Use index to intercept substring---------')

word = 'world'

print(word[4])

[Code Description] The fourth line of code accesses the value of the fifth character of the string. The output is "d".

【operation result】

 Regular interception of strings can be achieved through slicing. The syntax format of slices is as follows.

string[start : end : step]

[Code Description] where string represents the source string variable that needs to be substringed. [start:end:step] indicates that the substring is intercepted from the start index position to the end index of the string. The interception step is step. That is, intercept the string [start+step] each time until the end index. The index starts counting from 0.

The following code demonstrates the function of using slicing to intercept substrings:

 

#Special slice to intercept substring

print('---------# Special slice to intercept substring----------')

str1 = 'hello world'

print(str1[0:3])

print(str1[::2])

print(str1[1::2])

str2 = 'ABCDEFG'

print(str2[:3])

print(str2[3:])

print(str2[::2])

print('-----------------------------------')

[Code description]

The 10th line of code intercepts the part between the first character and the third character in the string.

In line 11 of code, the [::2] slice omits the start and end characters. Starting from the first character of the string, intercept characters one by one in steps of 2.

In the 12th line of code, the number 1 in the slice means taking characters starting from the second character of the string, and the number 2 means intercepting characters one by one in steps of 2.

【operation result】

 If you want to intercept multiple substrings at the same time, you can use the function split() to achieve this. The declaration of function split() is as follows.

split([char] [,num])

[Code description]

The parameter char represents the character used for separation. The default separation character is a space.

The parameter num represents the number of divisions. If num equals 2, the source string will be split into 3 substrings. By default, substrings are split based on the number of occurrences of the character char in the string.

The return value of the function is a list of substrings.

code show as below:

 

# Use split() to get substrings

print('-----------# Use split() to get the substring-----')

sentence = "Tom said: a,b,c,d."

print('Use spaces to get substrings:', sentence.split())

print('Use commas to get substrings:', sentence.split(','))

print('Use commas to get 3 substrings:', sentence.split(',', 2))

sentence1 = 'Uzi tell us : he is king and said : A, B, C, D.'

print('Use spaces to get substrings:', sentence1.split())

print('Use commas to get substrings:', sentence1.split(','))

print('Use spaces to get 4 substrings:', sentence1.split(' ',3))

print('-----------------------------------')

[Code description]

Line 22 of code retrieves substrings based on spaces. If there are 5 spaces in the string sentence, a list of 6 substrings will be returned.

Line 23 of code gets the substring based on the comma. If there are 3 spaces in the string sentence, a list of 4 substrings will be returned.

The 24th line of code splits the string based on commas and splits the string sentence into 3 substrings.

【operation result】

 

After string concatenation, Python will allocate new space to the concatenated string, and the source string remains unchanged.

 

# After string concatenation, Python will allocate new space to the concatenated string, and the source string remains unchanged.

print('After string concatenation, Python will allocate new space to the concatenated string, and the source string remains unchanged.')

str3 = "a"

print(id(str3))

print(id(str3 + "b"))

print('-------------------------------------')

[Code description]

Line 2 of code outputs the internal identifier of str1. The output result is "4337916312".

Line 3 of code performs string concatenation, and the new string will get a new identity. The output result is "4337800168".

【operation result】

 

ALL:

 

Running results ALL:

 

-------------------end--------------------

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/129617723