Python string search and replace methods
Python String Find and Replace Methods
The following are Python find and replace methods:
Number | Method and Description |
---|---|
1 | count(sub, beg ,end) Counts the number of times a substring sub occurs in a string. Given a starting index beg and an ending index end, it counts the number of times a substring occurs in a substring of a string. |
2 | find(sub, beg, end) Determines whether the substring sub occurs in string. Given a starting index beg and an ending index end, it searches for the substring within the string and returns the index if found; otherwise, it returns -1. |
3 | index(sub, beg, end) Similar to find(), but raises an exception if the string is not found. |
4 | replace(old, new [, max]) Replaces all occurrences of the old string old in a string with the new string new. If a maximum number of occurrences max is given, the replacement is performed at most max times. |
5 | rfind(sub, beg, end) Similar to find(), but searches from the end of the string to the beginning. |
6 | rindex(sub, beg, end) Similar to index(), but searches from the end of the string to the beginning. |
7 | startswith(sub, beg, end) Determines whether a string or a substring of a string (given a starting index beg and an ending index end) begins with the substring sub; if so, returns true; otherwise, returns false. |
8 | endswith(suffix, beg, end) Determines whether a string or a substring of a string (given a starting index beg and an ending index end) ends with the suffix suffix; returns true if so, false otherwise. |