• Register
1 vote
122 views

Problem:

I wrote this code to find all occurrences of a substring in a string python:

import re 
  
 
test_str = "all in all is not the all of all "
  

test_sub = "all"
  
 
print("The main string is : " + test_str) 
  
 
print("The substring to find : " + test_sub) 
  
  
  
 
print("The start indices of the substrings are : " + str(res)) 

 

The output of this code is:

The main string is : all in all is not the all of all 
The substring to find : all
Traceback (most recent call last):
  File "main.py", line 18, in <module>
    print("The start indices of the substrings are : " + str(res)) 
NameError: name 'res' is not defined

The first two ouput of this code is right but it throws error message after that.

What should I do to run this code properly?

8 6 2
4,980 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

The finditer function of the regex library can help us perform the task of finding the occurrences of the substring in the target string and the start function can return the resultant index of each of them. 

You are getting this error message because you do not have any function thats name is res.

So change your code to:
 

import re 
  
 
test_str = "all in all is not the all of all "
  

test_sub = "all"
  
 
print("The main string is : " + test_str) 
  
 
print("The substring to find : " + test_sub) 
  
 
res = [i.start() for i in re.finditer(test_sub, test_str)] 
  
 
print("The start indices of the substrings are : " + str(res)) 

And its output is:

The main string is : all in all is not the all of all 
The substring to find : all
The start indices of the substrings are : [0, 7, 22, 29]

Hope this will help you to solve your probelm.

Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 5 views
5 views
Problem: As I mentioned in my previous query, I'm having difficulties with my learning; I've tried searching the web but haven't been able to find adequate information; could you assist me in continuing my project?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 7 views
7 views
Problem: how to I Solve this: find index of substring in string python HELP! I need help understanding this: find index of substring in string python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
1 answer 4 views
4 views
Problem: The operation must be exported or contain an intent-filter, which is why I got the error of Find a substring in a string.
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 5 views
5 views
Problem: Could anyone kindly assist me in resolving this uncaught error? &ldquo;Find all substrings of a string python&rdquo;
asked Mar 29 tuhin1 48.9k points
1 vote
1 answer 22 views
22 views
Problem: Hello, good people of Kodlogs, I am a new student of Python programming language. The online course I am taking they are teaching me well. Every weekend they provide me with some problems to solve. This week, I have got the problem find all substrings of a string python. I don&rsquo;t have any idea how to do this! I would appreciate your effort if you pull me up. Many thanks
asked Apr 5, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
Problem: I can't seem to find a solution; could you please help me &ldquo;Find if a string is a substring of another java&rdquo;?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 18 views
18 views
Problem: How to find a substring in a string java. Is there any idea?
asked Mar 21 charles mathews 5.5k points
0 votes
1 answer 10 views
10 views
Problem: how to I Solve this: get all substrings of a string python HELP! I need help understanding this: get all substrings of a string python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
1 answer 3 views
3 views
Problem: Is there a way to substring a string in Python, to get a new string from the third character to the end of the string? Maybe like myString[2:end]?
asked Apr 18 ummesalma 29.2k points
0 votes
1 answer 6 views
6 views
Problem: I'll make a submission for a response to my question. Please, I have searched the internet but have not found any useful material, and I am now having trouble continuing my studies.
asked Mar 29 tuhin1 48.9k points