May 2019
Beginner
528 pages
29h 51m
English
You can search in a string for one or more adjacent characters—known as a substring—to count the number of occurrences, determine whether a string contains a substring, or determine the index at which a substring resides in a string. Each method shown in this section compares characters lexicographically using their underlying numeric values.
String method count returns the number of times its argument occurs in the string on which the method is called:
In [1]: sentence = 'to be or not to be that is the question'In [2]: sentence.count('to')Out[2]: 2
If you specify as the second argument a start_index, count searches only the slice string[start_index:]—that is, from start_index through end ...
Read now
Unlock full access