- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Does Python have a string 'contains' substring method?
No python doesn't have a 'contains' substring method. Instead you could use either of the below 2 methods:
Python has a keyword 'in' for finding if a string is a substring of another string. For example,
>>> 'ello' in 'hello world' True >>> 'no' in 'hello' False
If you also need the first index of the substring, you can use find(substr) to find the index. If this method returns -1, it means that substring doesn't exist in the string. For example,
>>> 'hello world'.find('ello') 1 >>> 'hello'.find('no') -1
- Related Articles
- What does the 'b' character do in front of a string literal in Python?
- How does $('iframe').ready() method work in jQuery?
- What does colon ':' operator do in Python?
- What does 'is' operator do in Python?
- What does 'in' operator do in Python?
- Program to find number of string we can make where 'a' can be 'a' or 'b', and 'b' remains 'b'in Python
- What does 'is not' operator do in Python?
- What does 'not in' operator do in Python?
- How does the 'in' operator work on a tuple in Python?
- Why does MySQL refuse pipe ('|') character in string on INSERT INTO?
- How does parameters 'c' and 'cmap' behave in a Matplotlib scatter plot?
- How and why does 'z'['toUpperCase']() in JavaScript work?
- How to do string concatenation without '+' operator in Python?
- C++ code to get updated string with same 'a' and 'b' count
- What does the 'b' modifier do when a file is opened using Python?

Advertisements