How to check if a string contains a substring in Python?
There are multiple ways to check if a string contains a specific substring.
Using in keyword
You can use in keyword to check if a string contains a specific substring. The expression will return boolean value.
The general syntax is the following:
Using the String.index
You can also use String.index method to find specific the index in the string where the substring starts.
Using the String.find
Also, you can use the find function from the String class. This function will return -1 if the substring is not present in the string.
-
Difference between static and class methods in Python?
Class method To create a class method, use the @classmethod decorator. Class methods receive the class as an implicit first argument, just like an instance method receives the instance. The class m...
Questions -
How to flatten a list in Python?
You can flatten a list in python using the following one-liner: flat_list = [item for sublist in l for item in sublist] In the example above, l is the list of lists that is to be flattened. The pre...
Questions -
What Does the “yield” Keyword Do in Python?
To better understand what yield does, you need first to understand what generator and iterable are. What is iterable When you use a list or list-like structure, you can read the values from the lis...
Questions -
Understanding slicing in Python?
Slicing is a way of extracting a specific part of an array. The syntax is following: mylist[start:end] # items start through end-1 mylist[start:] # items start through the rest of the array ...
Questions