Python: Search Substring
By Xah Lee. Date: .
Search Substring
str.startswith(prefix, ?start, ?end)
-
Return
True
if string starts with the prefix, else False
.
- prefix can be a
Tuple
, each element is a string.
- Optional ?start, ?end limits the range of positions to check.
str.endswith(suffix, ?start, ?end)
-
Similar to
startwith
, but check the end.
str.find(substr, ?start, ?end)
-
Return index of the first occurrence of substr. Return -1 if substr is not found.
str.rfind(substr, ?start, ?end)
-
Similar to
find
, but start at right.
str.index(substr, ?start, ?end)
-
Similar to
find
, but raise ValueError when the substring is not found.
str.rindex(substr, ?start, ?end)
-
Similar to
index
but start from right.
str.count(substr, ?start, ?end)
-
Return the count of non-overlapping occurrences of substring.