Does Python have a string contains substring method

0 votes

I want to do:

if not somestring.contains("blah"):
   continue

Feb 16, 2022 in Python by Dev
• 6,000 points
304 views

1 answer to this question.

0 votes

You can use in operator to access sub strings in Python.
in returns boolean value either True or False.

For Example:
statement = 'I am answering this question'
'I' in statement

Output

True
'not' in statement

Output

False

Another way is to use indexing str.find (which returns -1 on failure and returns the index position of the string on success )

start = 0
end = len(statement)
statement.find('not', start, end)

Output

-1
statement.find('I', start, end)

Output

0
answered Feb 16, 2022 by Nandini
• 5,480 points

Related Questions In Python

0 votes
1 answer

Does Python have a string 'contains' substring method?

You can use the in operator: if "blah" not in ...READ MORE

answered Nov 26, 2020 in Python by Gitika
• 65,910 points
280 views
+4 votes
6 answers

Does Python have a ternary conditional operator like C does?

Yes, the syntax is: a if condition else ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,878 views
0 votes
1 answer

How does Python know whether a variable in the class is a method or a variable?

In python objects/variables are wrapped into methods ...READ MORE

answered Sep 18, 2018 in Python by aryya
• 7,450 points
936 views
0 votes
1 answer

To simulate construct of other language does Python have a ternary conditional operator?

You can index into a tuple: (falseValue, trueValue)[test] test needs ...READ MORE

answered Oct 15, 2018 in Python by findingbugs
• 4,780 points
405 views
0 votes
1 answer

Is there a string 'contains' in python?

In this case, you can use the ...READ MORE

answered Sep 25, 2018 in Python by SDeb
• 13,300 points
504 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer

Is there a label/goto in Python?

Since Python is a structured programming language, ...READ MORE

answered Feb 4, 2022 in Python by Nandini
• 5,480 points
355 views
0 votes
1 answer

Is there a way to create multiline comments in Python?

In Python, you can use '''  some ...READ MORE

answered Feb 7, 2022 in Python by Nandini
• 5,480 points
244 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP