python string splicing. join of and splitting. split of detailed explanation
- 2021-12-13 08:53:02
1. String splitting function.split ()
Split the string by space:
s = "fd as sf" res = s.split(" ") print(res)
The return value is:
s.split(" ")Common words in splitting a sentence. The return value after splitting is
2. String splicing function.join ()
Splice strings according to spaces (which can be any characters):
l = ['1','12','s'] res = " ".join(l) print(res)
The result returned is: '1 12 s'
The return value after splicing is str type!
Extension: Understanding "". join (s. split ("") [::-1]) [::-1]
s = "I love you" res = " ".join(s.split(" ")[::-1])[::-1] print(res)
The return value is: "I evol uoy"
z 'z means that given a string, you need to reverse the character order of each word in the string, while still preserving the spaces and the initial order of the words.
This sentence forces the solution of question 557: 557. Invert the word III in the string
In addition to the above.
Besides, it also involves the usage of python slice.
Readers can understand the meaning of this sentence by themselves (or see the official analysis of Likou).
Remarks: For details of python slice analysis, please refer to the blog post: Python slice operation for in-depth detailed explanation
The following are some excerpts:
A complete slice expression contains two ":" to separate the three parameters (start_index, end_index, step). When there is only one ":", the third parameter step=1 by default; When there is no one ":", start_index=end_index means that the element specified by start_index is cut.
Basic expression of slicing operation: object[start_index:end_index:step]
Both positive and negative numbers can be used. The absolute value determines the step size when cutting data, while the sign determines the cutting direction. Positive means "from left to right" and negative means "from right to left". When step is omitted, it defaults to 1, that is, the step size is 1 from left to right. "Cutting direction is very important!" "Cutting direction is very important!" "Cutting direction is very important!" Say important things three times!
Represents the starting index (including the corresponding value of the index); When this parameter is omitted, it means that the value starts from the "endpoint" of the object. Whether it starts from the "starting point" or the "ending point" is determined by the plus or minus of the step parameter. If step is positive, it starts from the "starting point" and if it is negative, it starts from the "ending point".
Indicates the termination index (excluding the corresponding value of the index); When this parameter is omitted, it means that 1 goes straight to the data "endpoint". Whether it goes to the "starting point" or the "end point" is also determined by the plus or minus of step parameter. When step is positive until the "end point", when it is negative until the "starting point".
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!