The join () method is also a very important string method. It is the inverse of the split () method and is used to join multiple strings contained in a list (or tuple) into a single string.
Readers who want to know more about the split() method can read the "Python split () method" section.
When you use the join() method to merge strings, it joins multiple strings in a list (or tuple) together with a fixed separator. For example, the string "www.freelearningpoints.com" can be seen as the result of merging the list of ['www', 'freelearningpoints', 'com'] into one string with the separator ".".
The syntax of the join () method is as follows:
new_string = str.join(iterable)
The meaning of each parameter in this method is as follows:
[Example] Combine the strings in the list into one string.
a = ["www", "freelearningpoints", "com"] type(a) #check the class of a '.'.join(a)
The output is:
list
'www.freelearningpoints.com'
[Example] Combine the strings in the tuple into one string.
b = '','user','desktop','python' type(b) #check the class of a '/'.join(b)
The output is:
tuple
'/user/desktop/python'
The count method is used to retrieve the number of occurrences of the specified string in another string. If the retrieved string does not exist, it returns 0, otherwise it returns the number of occurrences.
The syntax of the count method is as follows:
str.count(sub[,start[,end]])
The specific meaning of each parameter in this method is as follows:
[Example] Retrieve the number of occurrences of "e" in the string "www.freelearningpoints.com".
string = "www.freelearningpoints.com" string.count('e') string = "www.freelearningpoints.com" string.count('e', 1) string.count('e', 2)
The output is:
3
3
2
As mentioned earlier, the search value corresponding to each character in the string starts from 0, so the search value 1 in this example corresponds to the second character '.', Which can be analyzed from the output result, and the search starts from the specified index position. Which also contains this index position.
[Example]
string = "www.freelearningpoints.com" string.count('e', 2, -3) string.count('e', 2, -4)
The output is:
3
2
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises