Как найти вхождение подстроки в строку python

Python find() – How to Search for a Substring in a String

When you’re working with a Python program, you might need to search for and locate a specific string inside another string.

This is where Python’s built-in string methods come in handy.

In this article, you will learn how to use Python’s built-in find() string method to help you search for a substring inside a string.

Here is what we will cover:

  1. Syntax of the find() method
    1. How to use find() with no start and end parameters example
    2. How to use find() with start and end parameters example
    3. Substring not found example
    4. Is the find() method case-sensitive?
  2. find() vs in keyword
  3. find() vs index()

The find() Method – A Syntax Overview

The find() string method is built into Python’s standard library.

It takes a substring as input and finds its index – that is, the position of the substring inside the string you call the method on.

The general syntax for the find() method looks something like this:

string_object.find("substring", start_index_number, end_index_number)

Let’s break it down:

  • string_object is the original string you are working with and the string you will call the find() method on. This could be any word you want to search through.
  • The find() method takes three parameters – one required and two optional.
  • "substring" is the first required parameter. This is the substring you are trying to find inside string_object. Make sure to include quotation marks.
  • start_index_number is the second parameter and it’s optional. It specifies the starting index and the position from which the search will start. The default value is 0.
  • end_index_number is the third parameter and it’s also optional. It specifies the end index and where the search will stop. The default is the length of the string.
  • Both the start_index_number and the end_index_number specify the range over which the search will take place and they narrow the search down to a particular section.

The return value of the find() method is an integer value.

If the substring is present in the string, find() returns the index, or the character position, of the first occurrence of the specified substring from that given string.

If the substring you are searching for is not present in the string, then find() will return -1. It will not throw an exception.

How to Use find() with No Start and End Parameters Example

The following examples illustrate how to use the find() method using the only required parameter – the substring you want to search.

You can take a single word and search to find the index number of a specific letter:

fave_phrase = "Hello world!"

# find the index of the letter 'w'
search_fave_phrase = fave_phrase.find("w")

print(search_fave_phrase)

#output

# 6

I created a variable named fave_phrase and stored the string Hello world!.

I called the find() method on the variable containing the string and searched for the letter ‘w’ inside Hello world!.

I stored the result of the operation in a variable named search_fave_phrase and then printed its contents to the console.

The return value was the index of w which in this case was the integer 6.

Keep in mind that indexing in programming and Computer Science in general always starts at 0 and not 1.

How to Use find() with Start and End Parameters Example

Using the start and end parameters with the find() method lets you limit your search.

For example, if you wanted to find the index of the letter ‘w’ and start the search from position 3 and not earlier, you would do the following:

fave_phrase = "Hello world!"

# find the index of the letter 'w' starting from position 3
search_fave_phrase = fave_phrase.find("w",3)

print(search_fave_phrase)

#output

# 6

Since the search starts at position 3, the return value will be the first instance of the string containing ‘w’ from that position and onwards.

You can also narrow down the search even more and be more specific with your search with the end parameter:

fave_phrase = "Hello world!"

# find the index of the letter 'w' between the positions 3 and 8
search_fave_phrase = fave_phrase.find("w",3,8)

print(search_fave_phrase)

#output

# 6

Substring Not Found Example

As mentioned earlier, if the substring you specify with find() is not present in the string, then the output will be -1 and not an exception.

fave_phrase = "Hello world!"

# search for the index of the letter 'a' in "Hello world"
search_fave_phrase = fave_phrase.find("a")

print(search_fave_phrase)

# -1

Is the find() Method Case-Sensitive?

What happens if you search for a letter in a different case?

fave_phrase = "Hello world!"

#search for the index of the letter 'W' capitalized
search_fave_phrase = fave_phrase.find("W")

print(search_fave_phrase)

#output

# -1

In an earlier example, I searched for the index of the letter w in the phrase “Hello world!” and the find() method returned its position.

In this case, searching for the letter W capitalized returns -1 – meaning the letter is not present in the string.

So, when searching for a substring with the find() method, remember that the search will be case-sensitive.

The find() Method vs the in Keyword – What’s the Difference?

Use the in keyword to check if the substring is present in the string in the first place.

The general syntax for the in keyword is the following:

substring in string

The in keyword returns a Boolean value – a value that is either True or False.

>>> "w" in "Hello world!"
True

The in operator returns True when the substring is present in the string.

And if the substring is not present, it returns False:

>>> "a" in "Hello world!"
False

Using the in keyword is a helpful first step before using the find() method.

You first check to see if a string contains a substring, and then you can use find() to find the position of the substring. That way, you know for sure that the substring is present.

So, use find() to find the index position of a substring inside a string and not to look if the substring is present in the string.

The find() Method vs the index() Method – What’s the Difference?

Similar to the find() method, the index() method is a string method used for finding the index of a substring inside a string.

So, both methods work in the same way.

The difference between the two methods is that the index() method raises an exception when the substring is not present in the string, in contrast to the find() method that returns the -1 value.

fave_phrase = "Hello world!"

# search for the index of the letter 'a' in 'Hello world!'
search_fave_phrase = fave_phrase.index("a")

print(search_fave_phrase)

#output

# Traceback (most recent call last):
#  File "/Users/dionysialemonaki/python_article/demopython.py", line 4, in <module>
#    search_fave_phrase = fave_phrase.index("a")
# ValueError: substring not found

The example above shows that index() throws a ValueError when the substring is not present.

You may want to use find() over index() when you don’t want to deal with catching and handling any exceptions in your programs.

Conclusion

And there you have it! You now know how to search for a substring in a string using the find() method.

I hope you found this tutorial helpful.

To learn more about the Python programming language, check out freeCodeCamp’s Python certification.

You’ll start from the basics and learn in an interactive and beginner-friendly way. You’ll also build five projects at the end to put into practice and help reinforce your understanding of the concepts you learned.

Thank you for reading, and happy coding!

Happy coding!



Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Let us solve this general problem of finding if a particular piece of string is present in a larger string in different ways. This is a very common kind of problem every programmer comes across atleast once in his/her lifetime. This article gives various techniques to solve it. 

Method 1: Using in operator The in operator is the most generic, fastest method to check for a substring, the power of in operator in python is very well known and is used in many operations across the entire language. 

Python3

test_str = "GeeksforGeeks"

print ("Does for exists in GeeksforGeeks ? : ")

if "for" in test_str :

    print ("Yes, String found")

else :

    print ("No, String not found")

Output :

Does for exists in GeeksforGeeks ? : 
Yes, String found

Time Complexity: O(1)

Auxiliary Space: O(1)

Method 2 : Using str.find() str.find() method is generally used to get the lowest index at which the string occurs, but also returns -1, if string is not present, hence if any value returns >= 0, string is present, else not present. 
 

Python3

test_str = "GeeksforGeeks"

res = test_str.find("for")

if res >= 0:

    print ("for is present in GeeksforGeeks")

else :

    print ("for is not present in GeeksforGeeks")

Output :

for is present in GeeksforGeeks

Time Complexity: O(n)

Auxiliary Space: O(1)

Method 3 : Using str.index() This method can be used to performs the similar task, but like str.find(), it doesn’t return a value, but a ValueError if string is not present, hence catching the exception is the way to check for string in substring. 
 

Python3

test_str = "GeeksforGeeks"

try :

    res = test_str.index("forg")

    print ("forg exists in GeeksforGeeks")

except :

    print ("forg does not exists in GeeksforGeeks")

Output :

forg does not exists in GeeksforGeeks

Time Complexity: O(n)

Auxiliary Space: O(1)

Method 4 : Using operator.contains() This is lesser known method to check for substring in a string, this method is also effective in accomplishing this task of checking a string in a string. 
 

Python3

import operator

test_str = "GeeksforGeeks"

if operator.contains(test_str, "for"):

    print ("for is present in GeeksforGeeks")

else :

    print ("for is not present in GeeksforGeeks")

Output :

for is present in GeeksforGeeks

Time Complexity: O(n)

Auxiliary Space: O(1)

Method#5: Using String.count() function: This function is used to count the presence of element is the string. We can use this function to check the existence of string in large string. If string exist in string then it return some number else it return 0. 

Python3

test_str = "GeeksforGeeks"

temp = "for"

ans = test_str.count(temp)

if ans:

    print ("for is present in GeeksforGeeks")

else :

    print ("for is not present in GeeksforGeeks")

Output:

for is present in GeeksforGeeks

Time Complexity: O(n)

Auxiliary Space: O(1)

Method#6: Using re.search() function: re.search() function is used to search for an pattern in string. We can use sub-string as a pattern to search in string. 

Python3

import re

test_str = "GeeksforGeeks"

temp = "forg"

ans = re.search(temp, test_str)

if ans:

    print ("forg is present in GeeksforGeeks")

else :

    print ("forg is not present in GeeksforGeeks")

Output:

forg is not present in GeeksforGeeks

Time Complexity: O(n)

Auxiliary Space: O(1)

Method#7: Using list comprehension 

Python3

s="geeks for geeks"

s2="geeks"

print(["yes" if s2 in s else "no"])

Time Complexity: O(1)

Auxiliary Space: O(1)

Method #8: using any() function : The any() function, which returns True if any element in the iterable is True.

Step by Step Algorithm :

  1. Initialize two variables: string to the given string and substring to the given substring.
  2. Use a for loop to iterate through all possible substrings of the given string that are of the same length as the given substring.
  3. Check if the current substring is equal to the given substring using the equality operator ==.
  4. If the current substring is equal to the given substring, print that the substring is present in the given string and exit the loop.
  5. If the loop completes without finding any substring that is equal to the given substring, print that the substring is not present in the given string.

Python3

string = "GeekforGeeks"

substring = "for"

if any(string[i:i+len(substring)] == substring for i in range(len(string)-len(substring)+1)):

    print("for is present in GeeksforGeeks")

else:

    print("for is not present in GeeksforGeeks")

Output

for is present in GeeksforGeeks

Complexity Analysis : 

Time Complexity: O(n*m) where n is length of string and m is length of substring

This is because the code iterates through a sequence of integers generated by the range function that has length O(n-m), and for each integer in the sequence, the code checks if the corresponding substring of length m starting at that position is equal to the substring. This substring check takes O(m) time. So overall time complexity will be O((n-m)m) = O(n*m).

Auxiliary Space: O(1)

This is because we only use a few variables to store the input string, substring, and the boolean result of the check. The space used by these variables does not depend on the size of the input string or substring.

Last Updated :
08 Mar, 2023

Like Article

Save Article

Python_Deep_5.6_site-5020-7250df.png

В этой статье поговорим про строки в Python, особенности поиска, а также о том, как искать подстроку или символ в строке.

Python_Pro_970x90-20219-1c8674.png

Но сначала давайте вспомним основные методы для обработки строк в Python:
• isalpha(str): если строка в Python включает в себя лишь алфавитные символы, возвращается True;
• islower(str): True возвращается, если строка включает лишь символы в нижнем регистре;
• isupper(str): True, если символы строки в Python находятся в верхнем регистре;
• startswith(str): True, когда строка начинается с подстроки str;
• isdigit(str): True, когда каждый символ строки — цифра;
• endswith(str): True, когда строка в Python заканчивается на подстроку str;
• upper(): строка переводится в верхний регистр;
• lower(): строка переводится в нижний регистр;
• title(): для перевода начальных символов всех слов в строке в верхний регистр;
• capitalize(): для перевода первой буквы самого первого слова строки в верхний регистр;
• lstrip(): из строки в Python удаляются начальные пробелы;
• rstrip(): из строки в Python удаляются конечные пробелы;
• strip(): из строки в Python удаляются и начальные, и конечные пробелы;
• rjust(width): когда длина строки меньше, чем параметр width, слева добавляются пробелы, строка выравнивается по правому краю;
• ljust(width): когда длина строки в Python меньше, чем параметр width, справа от неё добавляются пробелы для дополнения значения width, при этом происходит выравнивание строки по левому краю;
• find(str[, start [, end]): происходит возвращение индекса подстроки в строку в Python. В том случае, если подстрока не найдена, выполняется возвращение числа -1;
• center(width): когда длина строки в Python меньше, чем параметр width, слева и справа добавляются пробелы (равномерно) для дополнения значения width, причём происходит выравнивание строки по центру;
• split([delimeter[, num]]): строку в Python разбиваем на подстроки в зависимости от разделителя;
• replace(old, new[, num]): в строке одна подстрока меняется на другую;
• join(strs): строки объединяются в одну строку, между ними вставляется определённый разделитель.

Обрабатываем строку в Python

Представим, что ожидается ввод числа с клавиатуры. Перед преобразованием введенной нами строки в число можно легко проверить, введено ли действительно число. Если это так, выполнится операция преобразования. Для обработки строки используем такой метод в Python, как isnumeric():

string = input("Введите какое-нибудь число: ")
if string.isnumeric():
    number = int(string)
    print(number)

Следующий пример позволяет удалять пробелы в конце и начале строки:

string = "   привет мир!  "
string = string.strip()
print(string)           # привет мир!

Так можно дополнить строку пробелами и выполнить выравнивание:

print("iPhone 7:", "52000".rjust(10))
print("Huawei P10:", "36000".rjust(10))

В консоли Python будет выведено следующее:

iPhone 7:      52000
Huawei P10:      36000

Поиск подстроки в строке

Чтобы в Python выполнить поиск в строке, используют метод find(). Он имеет три формы и возвращает индекс 1-го вхождения подстроки в строку:
• find(str): поиск подстроки str производится с начала строки и до её конца;
• find(str, start): с помощью параметра start задаётся начальный индекс, и именно с него и выполняется поиск;
• find(str, start, end): посредством параметра end задаётся конечный индекс, поиск выполняется до него.

Python_Pro_970x90-20219-1c8674.png

Когда подстрока не найдена, метод возвращает -1:

    welcome = "Hello world! Goodbye world!"
index = welcome.find("wor")
print(index)       # 6

# ищем с десятого индекса
index = welcome.find("wor",10)
print(index)       # 21

# ищем с 10-го по 15-й индекс
index = welcome.find("wor",10,15)
print(index)       # -1

Замена в строке

Чтобы в Python заменить в строке одну подстроку на другую, применяют метод replace():
• replace(old, new): подстрока old заменяется на new;
• replace(old, new, num): параметр num показывает, сколько вхождений подстроки old требуется заменить на new.

Пример замены в строке в Python:

    phone = "+1-234-567-89-10"

# дефисы меняются на пробелы
edited_phone = phone.replace("-", " ")
print(edited_phone)     # +1 234 567 89 10

# дефисы удаляются
edited_phone = phone.replace("-", "")
print(edited_phone)     # +12345678910

# меняется только первый дефис
edited_phone = phone.replace("-", "", 1)
print(edited_phone)     # +1234-567-89-10

Разделение на подстроки в Python

Для разделения в Python используется метод split(). В зависимости от разделителя он разбивает строку на перечень подстрок. В роли разделителя в данном случае может быть любой символ либо последовательность символов. Этот метод имеет следующие формы:
• split(): в роли разделителя применяется такой символ, как пробел;
• split(delimeter): в роли разделителя применяется delimeter;
• split(delimeter, num): параметром num указывается, какое количество вхождений delimeter применяется для разделения. При этом оставшаяся часть строки добавляется в перечень без разделения на подстроки.

Соединение строк в Python

Рассматривая простейшие операции со строками, мы увидели, как объединяются строки через операцию сложения. Однако есть и другая возможность для соединения строк — метод join():, объединяющий списки строк. В качестве разделителя используется текущая строка, у которой вызывается этот метод:

words = ["Let", "me", "speak", "from", "my", "heart", "in", "English"]

# символ разделителя - пробел
sentence = " ".join(words)
print(sentence)  # Let me speak from my heart in English

# символ разделителя - вертикальная черта
sentence = " | ".join(words)
print(sentence)  # Let | me | speak | from | my | heart | in | English

А если вместо списка в метод join передать простую строку, разделитель будет вставляться уже между символами:

word = "hello"
joined_word = "|".join(word)
print(joined_word)      # h|e|l|l|o

Python_Pro_970x550-20219-0846c7.png

There is no simple built-in string function that does what you’re looking for, but you could use the more powerful regular expressions:

import re
[m.start() for m in re.finditer('test', 'test test test test')]
#[0, 5, 10, 15]

If you want to find overlapping matches, lookahead will do that:

[m.start() for m in re.finditer('(?=tt)', 'ttt')]
#[0, 1]

If you want a reverse find-all without overlaps, you can combine positive and negative lookahead into an expression like this:

search = 'tt'
[m.start() for m in re.finditer('(?=%s)(?!.{1,%d}%s)' % (search, len(search)-1, search), 'ttt')]
#[1]

re.finditer returns a generator, so you could change the [] in the above to () to get a generator instead of a list which will be more efficient if you’re only iterating through the results once.

David Leon's user avatar

answered Jan 12, 2011 at 2:43

moinudin's user avatar

moinudinmoinudin

133k45 gold badges189 silver badges214 bronze badges

9

>>> help(str.find)
Help on method_descriptor:

find(...)
    S.find(sub [,start [,end]]) -> int

Thus, we can build it ourselves:

def find_all(a_str, sub):
    start = 0
    while True:
        start = a_str.find(sub, start)
        if start == -1: return
        yield start
        start += len(sub) # use start += 1 to find overlapping matches

list(find_all('spam spam spam spam', 'spam')) # [0, 5, 10, 15]

No temporary strings or regexes required.

Pratik Deoghare's user avatar

answered Jan 12, 2011 at 3:13

Karl Knechtel's user avatar

Karl KnechtelKarl Knechtel

61.4k11 gold badges97 silver badges146 bronze badges

6

Here’s a (very inefficient) way to get all (i.e. even overlapping) matches:

>>> string = "test test test test"
>>> [i for i in range(len(string)) if string.startswith('test', i)]
[0, 5, 10, 15]

answered Jan 12, 2011 at 2:48

thkala's user avatar

thkalathkala

83.4k23 gold badges155 silver badges199 bronze badges

3

Use re.finditer:

import re
sentence = input("Give me a sentence ")
word = input("What word would you like to find ")
for match in re.finditer(word, sentence):
    print (match.start(), match.end())

For word = "this" and sentence = "this is a sentence this this" this will yield the output:

(0, 4)
(19, 23)
(24, 28)

answered Feb 3, 2016 at 19:01

Idos's user avatar

IdosIdos

15k14 gold badges58 silver badges72 bronze badges

2

Again, old thread, but here’s my solution using a generator and plain str.find.

def findall(p, s):
    '''Yields all the positions of
    the pattern p in the string s.'''
    i = s.find(p)
    while i != -1:
        yield i
        i = s.find(p, i+1)

Example

x = 'banananassantana'
[(i, x[i:i+2]) for i in findall('na', x)]

returns

[(2, 'na'), (4, 'na'), (6, 'na'), (14, 'na')]

answered Dec 23, 2015 at 23:09

AkiRoss's user avatar

AkiRossAkiRoss

11.6k6 gold badges59 silver badges85 bronze badges

3

You can use re.finditer() for non-overlapping matches.

>>> import re
>>> aString = 'this is a string where the substring "is" is repeated several times'
>>> print [(a.start(), a.end()) for a in list(re.finditer('is', aString))]
[(2, 4), (5, 7), (38, 40), (42, 44)]

but won’t work for:

In [1]: aString="ababa"

In [2]: print [(a.start(), a.end()) for a in list(re.finditer('aba', aString))]
Output: [(0, 3)]

AnukuL's user avatar

AnukuL

5751 gold badge7 silver badges21 bronze badges

answered Jan 12, 2011 at 2:55

Chinmay Kanchi's user avatar

Chinmay KanchiChinmay Kanchi

62.1k22 gold badges86 silver badges114 bronze badges

2

Come, let us recurse together.

def locations_of_substring(string, substring):
    """Return a list of locations of a substring."""

    substring_length = len(substring)    
    def recurse(locations_found, start):
        location = string.find(substring, start)
        if location != -1:
            return recurse(locations_found + [location], location+substring_length)
        else:
            return locations_found

    return recurse([], 0)

print(locations_of_substring('this is a test for finding this and this', 'this'))
# prints [0, 27, 36]

No need for regular expressions this way.

answered Nov 1, 2013 at 3:16

Cody Piersall's user avatar

Cody PiersallCody Piersall

8,2142 gold badges42 silver badges57 bronze badges

2

If you’re just looking for a single character, this would work:

string = "dooobiedoobiedoobie"
match = 'o'
reduce(lambda count, char: count + 1 if char == match else count, string, 0)
# produces 7

Also,

string = "test test test test"
match = "test"
len(string.split(match)) - 1
# produces 4

My hunch is that neither of these (especially #2) is terribly performant.

answered Sep 24, 2014 at 21:12

jstaab's user avatar

jstaabjstaab

3,30925 silver badges40 bronze badges

1

this is an old thread but i got interested and wanted to share my solution.

def find_all(a_string, sub):
    result = []
    k = 0
    while k < len(a_string):
        k = a_string.find(sub, k)
        if k == -1:
            return result
        else:
            result.append(k)
            k += 1 #change to k += len(sub) to not search overlapping results
    return result

It should return a list of positions where the substring was found.
Please comment if you see an error or room for improvment.

answered Apr 1, 2015 at 9:23

Thurines's user avatar

ThurinesThurines

1111 silver badge3 bronze badges

This does the trick for me using re.finditer

import re

text = 'This is sample text to test if this pythonic '
       'program can serve as an indexing platform for '
       'finding words in a paragraph. It can give '
       'values as to where the word is located with the '
       'different examples as stated'

#  find all occurances of the word 'as' in the above text

find_the_word = re.finditer('as', text)

for match in find_the_word:
    print('start {}, end {}, search string '{}''.
          format(match.start(), match.end(), match.group()))

answered Jul 6, 2018 at 9:34

Bruno Vermeulen's user avatar

Bruno VermeulenBruno Vermeulen

2,8732 gold badges14 silver badges27 bronze badges

This thread is a little old but this worked for me:

numberString = "onetwothreefourfivesixseveneightninefiveten"
testString = "five"

marker = 0
while marker < len(numberString):
    try:
        print(numberString.index("five",marker))
        marker = numberString.index("five", marker) + 1
    except ValueError:
        print("String not found")
        marker = len(numberString)

wingerse's user avatar

wingerse

3,6301 gold badge27 silver badges57 bronze badges

answered Sep 1, 2014 at 12:48

Andrew H's user avatar

Andrew HAndrew H

46610 silver badges22 bronze badges

You can try :

>>> string = "test test test test"
>>> for index,value in enumerate(string):
    if string[index:index+(len("test"))] == "test":
        print index

0
5
10
15

answered Feb 27, 2018 at 6:44

Harsha Biyani's user avatar

Harsha BiyaniHarsha Biyani

7,0279 gold badges37 silver badges61 bronze badges

You can try :

import re
str1 = "This dress looks good; you have good taste in clothes."
substr = "good"
result = [_.start() for _ in re.finditer(substr, str1)]
# result = [17, 32]

answered Oct 25, 2021 at 10:13

Mohammad Amin Eskandari's user avatar

2

When looking for a large amount of key words in a document, use flashtext

from flashtext import KeywordProcessor
words = ['test', 'exam', 'quiz']
txt = 'this is a test'
kwp = KeywordProcessor()
kwp.add_keywords_from_list(words)
result = kwp.extract_keywords(txt, span_info=True)

Flashtext runs faster than regex on large list of search words.

answered Sep 28, 2018 at 17:29

Uri Goren's user avatar

Uri GorenUri Goren

13.2k6 gold badges57 silver badges109 bronze badges

This function does not look at all positions inside the string, it does not waste compute resources. My try:

def findAll(string,word):
    all_positions=[]
    next_pos=-1
    while True:
        next_pos=string.find(word,next_pos+1)
        if(next_pos<0):
            break
        all_positions.append(next_pos)
    return all_positions

to use it call it like this:

result=findAll('this word is a big word man how many words are there?','word')

answered Jan 13, 2020 at 12:39

Valentin Goikhman's user avatar

0

src = input() # we will find substring in this string
sub = input() # substring

res = []
pos = src.find(sub)
while pos != -1:
    res.append(pos)
    pos = src.find(sub, pos + 1)

answered May 16, 2020 at 17:05

mascai's user avatar

mascaimascai

1,1351 gold badge8 silver badges26 bronze badges

1

Whatever the solutions provided by others are completely based on the available method find() or any available methods.

What is the core basic algorithm to find all the occurrences of a
substring in a string?

def find_all(string,substring):
    """
    Function: Returning all the index of substring in a string
    Arguments: String and the search string
    Return:Returning a list
    """
    length = len(substring)
    c=0
    indexes = []
    while c < len(string):
        if string[c:c+length] == substring:
            indexes.append(c)
        c=c+1
    return indexes

You can also inherit str class to new class and can use this function
below.

class newstr(str):
def find_all(string,substring):
    """
    Function: Returning all the index of substring in a string
    Arguments: String and the search string
    Return:Returning a list
    """
    length = len(substring)
    c=0
    indexes = []
    while c < len(string):
        if string[c:c+length] == substring:
            indexes.append(c)
        c=c+1
    return indexes

Calling the method

newstr.find_all(‘Do you find this answer helpful? then upvote
this!’,’this’)

answered Feb 15, 2018 at 20:02

naveen raja's user avatar

This is solution of a similar question from hackerrank. I hope this could help you.

import re
a = input()
b = input()
if b not in a:
    print((-1,-1))
else:
    #create two list as
    start_indc = [m.start() for m in re.finditer('(?=' + b + ')', a)]
    for i in range(len(start_indc)):
        print((start_indc[i], start_indc[i]+len(b)-1))

Output:

aaadaa
aa
(0, 1)
(1, 2)
(4, 5)

darkByt3's user avatar

answered Jan 20, 2020 at 22:47

Ruman Khan's user avatar

if you want to use without re(regex) then:

find_all = lambda _str,_w : [ i for i in range(len(_str)) if _str.startswith(_w,i) ]

string = "test test test test"
print( find_all(string, 'test') ) # >>> [0, 5, 10, 15]

answered Nov 5, 2021 at 8:38

WangSung's user avatar

WangSungWangSung

2192 silver badges5 bronze badges

Here’s a solution that I came up with, using assignment expression (new feature since Python 3.8):

string = "test test test test"
phrase = "test"
start = -1
result = [(start := string.find(phrase, start + 1)) for _ in range(string.count(phrase))]

Output:

[0, 5, 10, 15]

answered Apr 8, 2022 at 10:06

Mike's user avatar

MikeMike

1132 silver badges6 bronze badges

I think the most clean way of solution is without libraries and yields:

def find_all_occurrences(string, sub):
    index_of_occurrences = []
    current_index = 0
    while True:
        current_index = string.find(sub, current_index)
        if current_index == -1:
            return index_of_occurrences
        else:
            index_of_occurrences.append(current_index)
            current_index += len(sub)

find_all_occurrences(string, substr)

Note: find() method returns -1 when it can’t find anything

SUTerliakov's user avatar

SUTerliakov

4,6973 gold badges14 silver badges36 bronze badges

answered Oct 13, 2022 at 20:06

ulas.kesik's user avatar

ulas.kesikulas.kesik

981 silver badge5 bronze badges

The pythonic way would be:

mystring = 'Hello World, this should work!'
find_all = lambda c,s: [x for x in range(c.find(s), len(c)) if c[x] == s]

# s represents the search string
# c represents the character string

find_all(mystring,'o')    # will return all positions of 'o'

[4, 7, 20, 26] 
>>> 

perror's user avatar

perror

6,96316 gold badges58 silver badges84 bronze badges

answered Apr 10, 2018 at 19:40

Harvey's user avatar

2

if you only want to use numpy here is a solution

import numpy as np

S= "test test test test"
S2 = 'test'
inds = np.cumsum([len(k)+len(S2) for k in S.split(S2)[:-1]])- len(S2)
print(inds)

answered Jun 10, 2021 at 16:46

Phillip Maire's user avatar

please look at below code

#!/usr/bin/env python
# coding:utf-8
'''黄哥Python'''


def get_substring_indices(text, s):
    result = [i for i in range(len(text)) if text.startswith(s, i)]
    return result


if __name__ == '__main__':
    text = "How much wood would a wood chuck chuck if a wood chuck could chuck wood?"
    s = 'wood'
    print get_substring_indices(text, s)

answered Mar 16, 2017 at 1:14

黄哥Python培训's user avatar

黄哥Python培训黄哥Python培训

2392 silver badges5 bronze badges

1

def find_index(string, let):
    enumerated = [place  for place, letter in enumerate(string) if letter == let]
    return enumerated

for example :

find_index("hey doode find d", "d") 

returns:

[4, 7, 13, 15]

Sabito stands with Ukraine's user avatar

answered Nov 8, 2020 at 13:49

Elli's user avatar

1

Not exactly what OP asked but you could also use the split function to get a list of where all the substrings don’t occur. OP didn’t specify the end goal of the code but if your goal is to remove the substrings anyways then this could be a simple one-liner. There are probably more efficient ways to do this with larger strings; regular expressions would be preferable in that case

# Extract all non-substrings
s = "an-example-string"
s_no_dash = s.split('-')
# >>> s_no_dash
# ['an', 'example', 'string']

# Or extract and join them into a sentence
s_no_dash2 = ' '.join(s.split('-'))
# >>> s_no_dash2
# 'an example string'

Did a brief skim of other answers so apologies if this is already up there.

answered May 19, 2021 at 13:43

als0052's user avatar

als0052als0052

3893 silver badges12 bronze badges

def count_substring(string, sub_string):
    c=0
    for i in range(0,len(string)-2):
        if string[i:i+len(sub_string)] == sub_string:
            c+=1
    return c

if __name__ == '__main__':
    string = input().strip()
    sub_string = input().strip()
    
    count = count_substring(string, sub_string)
    print(count)

answered Jun 2, 2021 at 3:24

CHANDANA SAMINENI's user avatar

2

I runned in the same problem and did this:

hw = 'Hello oh World!'
list_hw = list(hw)
o_in_hw = []

while True:
    o = hw.find('o')
    if o != -1:
        o_in_hw.append(o)
        list_hw[o] = ' '
        hw = ''.join(list_hw)
    else:
        print(o_in_hw)
        break

Im pretty new at coding so you can probably simplify it (and if planned to used continuously of course make it a function).

All and all it works as intended for what i was doing.

Edit: Please consider this is for single characters only, and it will change your variable, so you have to create a copy of the string in a new variable to save it, i didnt put it in the code cause its easy and its only to show how i made it work.

answered Jun 25, 2021 at 20:18

Lucas LP's user avatar

By slicing we find all the combinations possible and append them in a list and find the number of times it occurs using count function

s=input()
n=len(s)
l=[]
f=input()
print(s[0])
for i in range(0,n):
    for j in range(1,n+1):
        l.append(s[i:j])
if f in l:
    print(l.count(f))

barbsan's user avatar

barbsan

3,40811 gold badges21 silver badges28 bronze badges

answered Jul 30, 2019 at 11:44

BONTHA SREEVIDHYA's user avatar

2

To find all the occurence of a character in a give string and return as a dictionary
eg: hello
result :
{‘h’:1, ‘e’:1, ‘l’:2, ‘o’:1}

def count(string):
   result = {}
   if(string):
     for i in string:
       result[i] = string.count(i)
     return result
   return {}

or else you do like this

from collections import Counter

   def count(string):
      return Counter(string)

answered Apr 30, 2022 at 8:00

Aminu Aminaldo's user avatar

В этой статье мы расскажем о четырех способах найти подстроку в строке. У каждого способа есть плюсы и минусы — о них мы также кратко поговорим.

1) Оператор in

Простейший способ проверить наличие подстроки в строке — оператор in. Он используется для проверки наличия элемента в какой-либо структуре данных. Этот оператор возвращает булево значение — True или False. Пример:

fullstring = "pythonist"
substring = "python"

if substring in fullstring:
    print "Подстрока найдена!"
else:
    print "Подстрока не найдена!"

Этот оператор — сокращенный вызов метода __contains__. Он подходит и для проверки наличия элемента в списке. 

2) Метод String.index()

Тип string имеет метод index. Он возвращает наименьший индекс, по которому обнаруживается начало подстроки в строке. Если подстрока не найдена, то возбуждается исключение ValueError. Реализовать его можно с помощью блока try-except-else.

fullstring = "pythonist"
substring = "python"

try:
    fullstring.index(substring)
except ValueError:
    print "Подстрока не найдена!"
else:
    print "Подстрока найдена!"

Этот метод может особенно пригодиться, если вам нужно найти индекс подстроки, а не просто узнать о ее существовании.

3) Метод String.find()

Тип string имеет еще один метод — find. Он удобнее, чем index, потому что благодаря ему нам не нужно думать об обработке ошибок. Если метод не находит подстроку в строке, он возвращает -1. В случае успеха он возвращает наименьший индекс, по которому обнаруживается подстрока.

fullstring = "pythonist"
substring = "python"

if fullstring.find(substring) != -1:
    print "Подстрока найдена!"
else:
    print "Подстрока не найдена!"

Если вы не хотите обрабатывать ошибки — отдайте предпочтение этому методу, а не index.

4) Регулярные выражения (REGEX)

Регулярные выражения — более гибкий способ нахождения подстрок в строках. В Python есть встроенный модуль, позволяющий работать с регулярными выражениями, — re. Модуль re содержит функцию search, которая позволит вам найти подстроку: 

from re import search

fullstring = "pythonist"
substring = "python"

if search(substring, fullstring):
    print "Подстрока найдена!"
else:
    print "Подстрока не найдена!"

Если вам нужны сложные сопоставления, например, учет регистра — этот метод подойдет вам лучше всего. Но у него есть и недостатки: сложность и скорость работы. То есть, в простеньких задачах его лучше не использовать.

Добавить комментарий