How to check if a string starts with a substring using regex in Python?

Prerequisite: Regular Expression in Python

Given a string str, the task is to check if a string starts with a given substring or not using regular expression.

Examples:

Input: String: "geeks for geeks makes learning fun" 
       Substring: "geeks" 
Output: True 
Input: String: "geeks for geeks makes learning fun" 
       Substring: "makes" 
Output: False

Approach 1:
Here, we first check a given substring present in a string or not if yes then we use search() function of re library along with metacharacter “^”. This metacharacter checks for a given string starts with substring provided or not. 

Below is the implementation of the above approach:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import library
import re
  
# define a function 
def find(string, sample) :
    
  # check substring present 
  # in a string or not
  if (sample in string):
  
      y = "^" + sample
  
      # check if string starts 
      # with the substring
      x = re.search(y, string)
  
      if x :
          print("string starts with the given substring")
  
      else :
          print("string doesn't start with the given substring")
  
  else :
      print("entered string isn't a substring")
  
  
# Driver code
string = "geeks for geeks makes learning fun"  
sample = "geeks"
  
# function call
find(string, sample)
  
sample = "makes"
  
# function call
find(string, sample)

chevron_right


Output:

string starts with the given substring
string doesn't start with the given substring

Approach 2:
Here, we first check a given substring present in a string or not if yes then we use search() function of re library along with metacharacter “\A”. This metacharacter checks for a given string starts with substring provided or not.

 
Below is the implementation of the above approach:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import library
import re
  
# define a function 
def find(string, sample) :
    
  # check substring present 
  # in a string or not
  if (sample in string):
  
      y = "\A" + sample
  
      # check if string starts 
      # with the substring
      x = re.search(y, string)
  
      if x :
          print("string starts with the given substring")
  
      else :
          print("string doesn't start with the given substring")
  
  else :
      print("entered string isn't a substring")
  
  
# Driver code
string = "geeks for geeks makes learning fun"  
sample = "geeks"
  
# function call
find(string, sample)
  
sample = "makes"
  
# function call
find(string, sample)

chevron_right


Output:

string starts with the given substring
string doesn't start with the given substring

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.