Python | Check if String starts & ends with same characters

This article will check if a string starts and ends with the same character or sub-string.

For example,

  • The string “this and another word is that” : Starts and ends with same character ‘t’
  • The string “this and another word is this” : Starts and ends with same sub-string ‘this’
  • The string “11this55 and another word is 11this55” : Starts and ends with same sub-string ’11this55′
  • The string ” this and another word is that “ : Starts and ends with same character ‘ ‘ i.e. an empty space.

There are two ways to achieve this. Let’s discuss them one by one,

Check if String Starts & Ends with Same Characters using Regex

In Python, the regex module provides a function search(str, pattern). It accepts a string and regex pattern as arguments. Then it scans through the string and look for the given regex pattern. If a match to the pattern is found, then it returns the Match object, otherwise it returns None. We can use this to check if a string has the same character or sub-string at the start and end of the string. For this, we will use this regex pattern,

"^(.+).*$"

This pattern first looks at the start of the string using “^(.+)”. It selects one or more characters from the start of the string and creates a group of them. Then it checks if the string ends with same characters using “\1$”. Here “\1” points to the first group created by regex, i.e. “^(.+)”. It helps us to detect if the string has the same set of characters at the start and end. Let’s understand with some examples,

Example:

import re

def start_and_end_with_same(sample_str):
    ''' Returns True if the String Starts and Ends with
        Same Characters, otherwise returns False'''
    # Regex Pattern to match strings that similar
    # characters in start and end 
    pattern = r'^(.+).*$'
    return (re.search(pattern, sample_str) is not None)

print( start_and_end_with_same("this and another word is that") )
print( start_and_end_with_same("this and another word is  the") )
print( start_and_end_with_same("this and another word is  this") )
print( start_and_end_with_same("This is same as This") )
print( start_and_end_with_same("this and another word is  is") )
print( start_and_end_with_same("11this55 and another word is 11this55") )
print( start_and_end_with_same("  this and another word is that ") )

Output:

True
False
True
True
False
True
True

Analysis if the Output:

  1. For string “this and another word is that”, the function start_and_end_with_same() returned True because the string starts and ends with same character “t”.
  2. For string “this and another word is the”, the function start_and_end_with_same() returned False because the string did not have similar characters at the start and end.
  3. For string “this and another word is this”, the function start_and_end_with_same() returned True because the string starts and ends with same sub-string “this”.
  4. For string “This is same as This”, the function start_and_end_with_same() returned True because the string starts and ends with same sub-string “this”.
  5. For string “this and another word is”, the function start_and_end_with_same() returned False because the string did not have similar characters at the start and end.
  6. For string “11this55 and another word is 11this55”, the function start_and_end_with_same() returned True because the string starts and ends with same sub-string “11this55”.
  7. For string ” this and another word is that “, the function start_and_end_with_same() returned True because the string starts and ends with same character “_” i.e. an empty string.

There is another ways to do this i.e. with a different regex pattern

Check if String Starts & Ends with Same alphanumeric string using Regex

If you just want to check if string has same alphanumeric characters at the start and end, then we can use this regex pattern,

"^([a-zA-Z\d]+|[a-zA-Z\d]+]).*$"

This Pattern first looks at the start of the string using “^([a-zA-Z\d]+|[a-zA-Z\d]+])”. It selects one or more alphanumeric characters from the start of the string and creates a group of it. Then it checks if the string ends with same alphanumeric characters using “\1$”. Here “\1” points to the first group created by regex i.e. “^([a-zA-Z\d]+|[a-zA-Z\d]+])”. It helps us detect if the string has the same alphanumeric characters at the start and end.

Basically, it looks for the alphanumeric strings at the start and end of the string. But it will not be able to detect anything else like whitespace etc.

Let’s understand with some examples,

import re

def start_and_end_with_same(sample_str):
    ''' Returns True if the String Starts and Ends with
        Same Characters, otherwise returns False'''
    # Regex Pattern to match strings that similar
    # characters in start and end 
    pattern = r"^([a-zA-Z\d]+|[a-zA-Z\d]+]).*$"
    return (re.search(pattern, sample_str) is not None)

print( start_and_end_with_same("this and another word is that") )
print( start_and_end_with_same("this and another word is  the") )
print( start_and_end_with_same("this and another word is  this") )
print( start_and_end_with_same("This is same as This") )
print( start_and_end_with_same("this and another word is  is") )
print( start_and_end_with_same("11this55 and another word is 11this55") )
print( start_and_end_with_same("  this and another word is that ") )

Output:

True
False
True
True
False
True
False

Analysis if the Output:

  1. For string “this and another word is that”, the function start_and_end_with_same() returned True because the string starts and ends with same character “t”.
  2. For string “this and another word is the”, the function start_and_end_with_same() returned False because the string did not have similar characters at the start and end.
  3. For string “this and another word is this”, the function start_and_end_with_same() returned True because the string starts and ends with the same sub-string “this”.
  4. For string “This is same as This”, the function start_and_end_with_same() returned True because the string starts and ends with same sub-string “this”.
  5. For string “this and another word is is”, the function start_and_end_with_same() returned False because the string did not have the similar alphanumeric characters at the start and end.
  6. For string “11this55 and another word is 11this55”, the function start_and_end_with_same() returned True because the string starts and ends with same alphanumeric sub-string “11this55”.
  7. For string ” this and another word is that “, the function start_and_end_with_same() returned False because the string did not have the similar alphanumeric characters at the start and end.

Summary

We learned two different ways to test if a string starts with the same and ends with the same characters in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top