Check if string contains substring
Use the ‘in’ keyword to check if string contains substring in python.
# Let's creat a string
str1 = 'We will learn Data Science'
str2 = 'Data Science'
str3 = 'I will'
# We want to check if str1 contains str2
if str2 in str1:
print('Yes str1 contains str2')
# Now let's check if str1 contains str3
if str3 in str1:
print('Yes str1 contains str3')
else:
print('No str1 does not contain str3')
Yes str1 contains str2 No str1 does not contain str3
That's how we Check if string contains substring.
That’s all for this mini tutorial. To sum it up, we learned how to Check if string contains substring.
Hope it was easy, cool and simple to follow. Now it’s on you.
Related Resources:
- Yield Keyword Python Yield Keyword in Python Yield keyword when used inside a function definition returns a generator. Generator objects generate one value...
- String Cases | Learn to use lower and upper case String Cases String method lower() converts string to all lower case. String method upper() converts string to all upper case. ...
- Check if file or directory exists in Python Check if file or directory exists Use “path.exists” method of os library to check if file or directory exists for...
- Empty List | Learn to check if a list is empty Check if a list is empty The most pythonic and the simplest way is to use the fact “boolean value...
- Concept of global variable in python Concept of Global Variable Declare global variable the same way we declare a variable. Use global keyword to change global...
- Learn Python easily with Detailed tutorials Python Learn python easily and efficiently with mini tutorials one snippet at a time. Python is widely used language for...
- Difference between append and extend in python Difference between append and extend Append adds an object to the end of a list whereas extend merges elements to...
- Create a list of files in a folder python Create a list of files in a folder python Create a list of files in a folder python | List all...
- List comprehension python | Create lists elegantly List comprehension List comprehension python explained with easy tutorial. Elegant way to create lists in python. # Given list ls...
- Dictionary comprehension python Dictionary comprehension Dictionary comprehension python explained with easy tutorial. Elegant way to create dictionaries in python. # Given list friends...