How to check if a substring is present in a string using Python?

0 votes
a = ‘abcd’
found=a.find(‘b’)
print(found)

This is printing the location of the found substring. I want to only check if the substring is present in the string or not. How can I do this?

May 9 in Python by Tina
64 views

1 answer to this question.

0 votes

To check if the substring exists in the string or no, try the following code:

if ‘b’ in a:
    print(‘Substring found’)
else:
    print(‘Not found’)
answered May 9 by Sharan

Related Questions In Python

+1 vote
1 answer

How to check if a string is in a file, using python?

Hi, Try the below given code: with open('myfile.txt') as ...READ MORE

answered Jul 4 in Python by Shabnam
• 920 points
56 views
+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 56,940 points
4,341 views
0 votes
1 answer

How do I check if input string is a valid regular expression or not in Python?

Hi. Good question! Well, just like what ...READ MORE

answered Feb 12 in Python by Nymeria
• 3,520 points
137 views
0 votes
1 answer

How to check if a string ends with a character in python?

You can use the endswith method. It checks ...READ MORE

answered Apr 4 in Python by Srisha
64 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 350 views
+4 votes
6 answers
0 votes
1 answer

How to check if a list is empty in python?

To check if a list is empty ...READ MORE

answered May 27 in Python by Nisa
• 1,090 points
50 views
0 votes
1 answer

How to check if a given string matches a particular pattern in Python?

You can use re module of python ...READ MORE

answered Jul 3 in Python by Neel
• 3,010 points
40 views