# Functions based on searching and replacing a substring in a string

### Contents

Search other websites:

##### 1. The function str.count(). The number of substring occurrences in a given range

The str.count() function returns the number of occurrences of a substring in a given range. According to Python documentation, the general form of using a function is as follows

`n = str.count(substring[, start[, end]])`

here

• n – result, the number of occurrences of substring in string str that do not overlap;
• str – the original string;
• substring – a substring that may be included in the string str;
• start, end – respectively, the starting and ending positions (indices) in the string str, which determine the range that is taken into account (considered). In other words, the start, end values determine the slice.

Example.

```# Function str.count() - the number of occurrences of a substring in a given range

# 1. Function call, indicating the range
s1 = 'abcdef' # the original string
n = s1.count('bcd', 0, len(s1)) # n = 1, range 0..5

n = 'ab ab babab'.count('ab', 2, 10) # n = 2
n = 'ab ab babab'.count('ab', 0, 6) # n = 2

# 2. Calling a function without specifying a range
s1 = 'Hello world!'
n = s1.count('о') # n = 2, number of characters 'о' in the string

s1 = 'abcbcd abcd'
n = s1.count("bc") # n = 3

# 3. Function call, indicating the initial value start
s1 = 'abc abc abcd'
n = s1.count('abc', 3) # n = 2```

### ⇑

##### 2. Function str.find(). Search for a substring in a string

The find() function is designed to search for a substring in a string. According to the Python documentation, the general form of a function call is as follows:

`index = s.find(sub [, start[, end]])`

where

• index – integer value, which is the index of the first occurrence of the substring sub in string s. If the substring sub is not found in the string s, then index = -1;
• s – the string in which the substring sub is searched;
• start, end – positions in string s. These positions are determined by the boundaries of the cut s[start: end], which determines the selected range. If you do not specify start, end parameters, then the search is performed in the entire string.

Example.

```# Function str.find() - search for a substring in a string

# Source string to search in
s = 'abcde fg hijkl mnop'

# 1. Processing of an entire string
index = s.find('hij') # index = 9
index = s.find('+-=') # index = -1

# 2. Processing the slice s[start:end]
# part of the 'abcde' string s is taken into account
index = s.find('hij', 0, 5) # index = -1
index = s.find('cde', 0, 5) # index = 2

index = 'hello world!'.find('wor', 2, len(s)) # index = 6```

### ⇑

##### 3. Function str.index(). Searching a substring in the string to generate an exception

The str.index() function searches for a substring in a string. This function works the same as the str.find() function, however, if a substring is not found, a ValueError exception is thrown.

According to Python documentation, the general form of a function call is as follows:

`pos = str.index(substring [, start [, end]])`

here

• pos – the position of the substring in the string str in case the string is found. If the substring is not found, then a ValueError exception is thrown;
• str – string in which the search is performed;
• start, end – respectively, the start and end positions in the string str, which determine the search range.

Example.

```# Function str.index() - search for a substring in a string

#t = str.index('sdf', 'abcdef def hj') - exception

# Case 2. A substring exists in a string
s = 'abcdef'
t = s.index('bc') # t = 1 - position of the substring found

# Search in a given range
s = 'abc def ghi def'
t = s.index('def', 0, len(s)) # t = 4

# Search in the range of '012'
s = '0123456789'
t = s.index('012', 0, 3) # t = 0```

### ⇑

##### 4. Function str.rfind(). Find the largest index of substring occurrence in a string

The str.rfind() function returns the largest position (index) in the string of the given substring, if one is found. The general form of using the function is as follows:

`position = str.rfind(subs[, start[, end]])`

here

• position – the desired position (index) of the occurrence of the substring subs in string str. If the substring subs is not found in the string str, then position = -1;
• str – the string in which the search for the substring subs is performed;
• subs – given substring;
• start, end – respectively, the starting and ending indices defining the slice in the string str.

Example.

```# Function str.rfind()

# 1. Use without specifying a range
s1 = 'abc def ab abc'
index = s1.rfind('ab') # index = 11
index = s1.rfind('jkl') # index = -1

# 2. Using indicating the beginning of the start
s1 = '012 345 012'
index = s1.rfind('01', 3) # index = 8

# 3. Using indicating the beginning of the start and the end
s1 = 'abc def abc def gh'
s2 = 'bc' # substring
index = s1.rfind(s2, 0, len(s1)) # index = 9
index = s1.rfind(s2, 0, 1) # index = -1, substring is not found```

### ⇑

##### 5. Function str.rindex(). Find the largest index of substring occurrence in a string with a ValueError exception

The str.rindex() function works the same as the rfind() function, that is, it returns the largest position of the substring in the string. The difference between rindex() and rfind() is this: if a substring is not found in the string, a ValueError exception is thrown.

The general form of using the function is as follows:

`position = str.rindex(subs[, start[, end]])`

here

• position – the desired position (index) of the occurrence of the substring subs in string str. If the subs substring in str is not found, a ValueError exception is thrown;
• str – the string in which the search for the substring subs is performed;
• subs – given substring;
• start, end – respectively, the starting and ending indices defining the slice in the string str.

Example.

```# Function str.index()

# 1. The use without range
s1 = 'abc def ab abc'
index = s1.rindex('ab') # index = 11
index = str.rindex(s1, ' ') # index = 10, space character in 10th position

# 2. Use indicating start start
s1 = '012 345 012'
index = s1.rindex('01', 3) # index = 8

# 3. Using indicating the beginning of the start and the end
s1 = 'abc def abc def gh'
s2 = 'bc' # substring
index = s1.rindex(s2, 0, len(s1)) # index = 9

# The following code will throw an exception
index = s1.rindex(s2, 0, 1)```

### ⇑

##### 6. Function str.replace(). Replacing a substring in a string

The str.replace() function returns a copy of the string in which all occurrences of the substring old are replaced by new.

General form of using a function

`s1 = s2.replace(old, new[, count])`

here

• s1 – resulting string;
• s2 – the original string, in which substitutions of the substring old are made with the substring new;
• old – a substring that can be replaced with another substring new. The number of characters in the substring is arbitrary. If the substring old is not found in the string s2, then the function returns the string s2 without changes;
• new – a substring replacing the substring old in the string s2;
• count – the number of replacements that can be made. If count is not specified, then replacements are made in all possible occurrences of the substring old in string s2.

Example.

```# Function str.replace() - replacing a substring in a string

# 1. Call without using the count parameter
s1 = 'abcdef'
s2 = s1.replace('bc', '111') # s2 = 'a111def'

s1 = 'abc abc abc'
s2 = s1.replace('bc', '0000') # s2 = 'a0000 a0000 a0000'

# The case if the substring in the string is not found
s2 = str.replace(s1, 'jkl', '111') # s2 = 'abcdef'

# 2. Call using the count parameter
s1 = 'abcd abcd abcd'
s2 = s1.replace('bcd', '+++', 3) # s2 = 'a+++ a+++ a+++'
s2 = s1.replace('bcd', '++++', 2) # s2 = 'a++++ a++++ abcd'
s2 = s1.replace('ab', '---', 1) # s2 = '---cd abcd abcd'
s2 = s1.replace('abcd', '==', 6) # s2 = '== == =='```