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 a 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 first occurrence, so using this function this problem can be solved.
# function to check if small string is # there in big string def check(string, sub_str): if (string.find(sub_str) == -1): print("NO") else: print("YES") # driver code string = "geeks for geeks"sub_str ="geek"check(string, sub_str) |
chevron_right
filter_none
Output:
YES
Recommended Posts:
- Python | Check if substring present in string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Check whether the Average Character of the String is present or not
- Queries to check if string B exists as substring in string A
- Check if a string is substring of another
- Python | Check if element is present in tuple of tuples
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Check if string can be made lexicographically smaller by reversing any substring
- Python | All occurrences of substring in string
- Python | Get the string after occurrence of given substring
- Python | Remove the given substring from end of string
- Python | Frequency of substring in given string
- Python | Check if substring is part of List of Strings
- Python | Get the substring from given string using list slicing
- Python | Count overlapping substring in a given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



