Python | Check if a Substring is Present in a Given String
Given two strings, check if s1 is there in s2.
Examples:
Input : s1 = geeks s2=geeks for geeks Output : yes Input : s1 = geek s2=geeks for geeks Output : yes
We can iteratively check for every word, but Python provides us an inbuilt function find() which checks if a substring is present in the string, which is done in one line.
find() function returns -1 if it is not found, else it returns the first occurrence, so using this function this problem can be solved.
Method 1: Using user defined function.
Output:
YES
Method 2: Using “count()” method:-
Output:
YES
Method 3: Using regular expressions
RegEx can be used to check if a string contains the specified search pattern. Python has a built-in package called re, which can be used to work with Regular Expressions.
Output:
YES,string 'geek' is present in string 'A geek in need is a geek indeed'
No comments:
Post a Comment
Your feedback is highly appreciated and will help us to improve our content.