Find and Replace Methods



Following are the Find and Replace methods in Python −

Sr.No. Method & Description

1

count(sub, beg ,end)

Counts how many times sub occurs in string or in a substring of string if starting index beg and ending index end are given.

2

find(sub, beg, end)

Determine if sub occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise.

3

index(sub, beg, end)

Same as find(), but raises an exception if str not found.

4

replace(old, new [, max])

Replaces all occurrences of old in string with new or at most max occurrences if max given.

5

rfind(sub, beg, end)

Same as find(), but search backwards in string.

6

rindex( sub, beg, end)

Same as index(), but search backwards in string.

7

startswith(sub, beg, end)

Determines if string or a substring of string (if starting index beg and ending index end are given) starts with substring sub; returns true if so and false otherwise.

8

endswith(suffix, beg, end)

Determines if string or a substring of string (if starting index beg and ending index end are given) ends with suffix; returns true if so and false otherwise.

python_string_methods.htm
Advertisements