Python – Check whether a string starts and ends with the same character or not (using Regular Expression)

Given a string. The task is to write a regular expression to check whether a string starts and ends with the same character.

Examples:

Input :  
abba
Output :  
Valid

Input :  
a
Output :  
Valid

Input :  
abc
Output :  
Invalid

Solution:
The input can be divide into 2 cases:

  • Single character string: All single character strings satisfies the condition that they start and end with the same character. The regex for a string with only 1 character will be-
    '^[a-z]$'
  • Multiple character string: Here we need to check whether the first and the last character is same or not. We do this using \1. The regex will be-
    '^([a-z]).*\1$'

The two regular expressions can be combined using |

'^[a-z]$|^([a-z]).*\1$'

In this program, we will use search() method of re module.

Below is the implementation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to check if a string starts
# and ends with the same charcter
  
# import re module as it provides
# support for regular expressions 
import re
  
# the regular  expression
regex = r'^[a-z]$|^([a-z]).*\1$'
  
# function for checking the string
def check(string):
  
    # pass the regualar expression 
    # and the string in the search() method 
    if(re.search(regex, string)):  
        print("Valid")  
    else:  
        print("Invalid")  
  
if __name__ == '__main__' :
  
    sample1 = "abba"
    sample2 = "a"
    sample3 = "abcd"
  
    check(sample1)
    check(sample2)
    check(sample3)

chevron_right


Output :

Valid
Valid
Invalid
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.