• Register
1 vote
91 views

Problem:

I was trying to check if string contains substring from list using python. So I wrote this code which gives me wrong results. The code I wrote is:

mainstring = "Allinall"
substring = "all"

if substring is mainstring:
    print ("String Found")
else:
    print ("String Not found")

after running this code its output is:
 

String Not found

 

But the answer is not right. What is wrong in my code. Cam anyone explain and how can I check if string contains substring from list.

8 6 2
4,980 points

Please log in or register to answer this question.

3 Answers

1 vote

Solution:

You can check if string contains substring from list using the in operator:

mainstring = "Allinall"
substring = "all"

if substring in mainstring:
    print ("String Found")
else:
    print ("String Not found")

 

And the output is:

String Found

 

We can also use the string.index method:

mainstring = "Allinall"
substring = "all"

try:
    mainstring.index(substring)
except ValueError:
    print ("String Not Found")
else:
    print ("String Found")

 

Hope this will help you and you understand the things. If not feel free to ask me any questions you want to know.

And you wanted to know the problem in your code. The main problem in your code is you used the is operator for that if your main string and sub string have to match properly to get the acurate result. In your code your substing and main string is different and you used the is operator that is why you are getting the wrong results.

Happy coding.

11 6 4
34,950 points
0 votes

Solution:

1. Employ find method

The find method inspects in case the string holds a substring. In case it does, the method returns the starting index of a substring inside the string; otherwise it returns -1.

Syntax

The usual syntax is :

string.find(substring)
a_string="Python Programming" 
substring1="Programming" 
substring2="Language" 
print("Check if "+a_string+" contains "+substring1+":")
print(a_string.find(substring1)) 
print("Check if "+a_string+" contains "+substring2+":")
print(a_string.find(substring2))

2. Employ the in operator

The in operator returns true in case the substring subsists in a string and false in case ​otherwise.

Syntax

The usual syntax is:

substring in string
a_string="Python Programming"
substring1="Programming"
substring2="Language"
print("Check if "+a_string+" contains "+substring1+":")
print(substring1 in a_string)
print("Check if "+a_string+" contains "+substring2+":")
print(substring2 in a_string)

3. Employ count

The count method returns the number of cases of a substring in the string. In case the substring is not traced in a string, the function returns 0.

Syntax

The usual syntax is :

string.count(substring)
a_string="Python Programming"
substring1="Programming"
substring2="Language"
print("Check if "+a_string+" contains "+substring1+":")
print(a_string.count(substring1))
print("Check if "+a_string+" contains "+substring2+":")
print(a_string.count(substring2))

Attempt this test:

any(substring in string for substring in substring_list)

It will return True in case some of the substrings in substring_list is seized in string.

Note that there is a Python analogue of Marc Gravell's answer in the connected question:

from itertools import imap
any(imap(s.__contains__, substring_list))

 

10 6 4
31,120 points
1 vote

Solution:

This is a very common kind of problem every programmer comes across at least once in his/her lifetime. Finding substring from a string is an important and regular facing situation when you work in the real-development field. As I can see you’ve tried using the in operator and misspelled the operator name. The in operator in python is very well known and is used in many operations across the entire language. You may do the same thing using the operator.contains() function. This function comes with the module operator:

 

import operator
mainstring = "Allinall"
if operator.contains(mainstring, "all"):
	print ("all is present in Allinall")
else :
	print ("all is not present in Allinall")

 

13 9 6
94,260 points

Related questions

0 votes
1 answer 45 views
45 views
I am new to this. I just want to know how to do that?
asked Nov 17, 2020 TeamScript 13.5k points
0 votes
1 answer 14 views
14 views
Problem: How can I check if a string contains another substring in javascript?Generally I would expect a method String.contains(), but there doesn't seem to be one.
asked Feb 12 sasha 16.2k points
0 votes
1 answer 18 views
18 views
Problem: how to I Solve this: python check if list contains elements of another list HELP! I need help understanding this: python check if list contains elements of another list Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
1 vote
1 answer 5 views
5 views
Problem: How to fix this error : python check if a string contains a substring
asked Mar 31 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 4 views
4 views
Problem: Please help me. How can I solve it? python check if string contains substring
asked Mar 30 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 164 views
164 views
Problem: I was trying to check if string contains special characters using a python program. To check this I wrote this code: import re # Function checks if the string # contains any special character def check(string): regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]' ... line 11, in check if(search(string) == None): NameError: name 'search' is not defined How can I solve this problem can anyone help?
asked Jun 10, 2020 samhaz 5k points
1 vote
1 answer 785 views
785 views
Problem: Hello Kodlogs, I am sure you guys are doing well. I just entered the world of Python programming. I&rsquo;ve got a problem to solve that says, &ldquo;python check if string contains only certain characters&ldquo; I tried several methods. Unfortunately, none ... worked for me. Could you please write a program that can check if a string contains only certain characters? Thanks in advance.
asked Mar 31, 2020 Gavin 15.3k points
1 vote
1 answer 2 views
2 views
Problem: How to fix this error : python check if list contains string
asked Mar 30 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 6 views
6 views
Problem: Can anyone assist me with this issue? >Check if a string is a substring of another string in java.
asked May 9 Florina Gulnar 8.5k points
1 vote
1 answer 8 views
8 views
Problem: I&rsquo;m trying to create a program in C to check if a string contains a substring or not . So I wanted to know what can I use instead of string::find in C++ ?
asked Apr 30 Raj Tyagi 15.6k points