Python - Check whether a string starts and ends with the same character or not

When it is required to check if a string begins and ends with the same character or not, there are multiple approaches. You can use regular expressions, simple string indexing, or built-in string methods.

Using Regular Expression

A method can be defined that uses the 'search' function to see if a string begins and ends with a specific character ?

import re

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

def check_string(my_string):
    if(re.search(regex_expression, my_string)):
        print("The given string starts and ends with the same character")
    else:
        print("The given string does not start and end with the same character")

my_string = "abcbabda"

print("The string is:")
print(my_string)
check_string(my_string)
The string is:
abcbabda
The given string starts and ends with the same character

Using String Indexing

A simpler approach using the first and last characters directly ?

def check_string_simple(my_string):
    if len(my_string) == 0:
        print("Empty string")
        return
    
    if my_string[0].lower() == my_string[-1].lower():
        print("The given string starts and ends with the same character")
    else:
        print("The given string does not start and end with the same character")

# Test with different strings
test_strings = ["abcba", "hello", "programming", "a", ""]

for string in test_strings:
    print(f"String: '{string}'")
    check_string_simple(string)
    print()
String: 'abcba'
The given string starts and ends with the same character

String: 'hello'
The given string does not start and end with the same character

String: 'programming'
The given string does not start and end with the same character

String: 'a'
The given string starts and ends with the same character

String: ''
Empty string

Using startswith() and endswith() Methods

Using built-in string methods for more readable code ?

def check_string_builtin(my_string):
    if len(my_string) == 0:
        return "Empty string"
    
    first_char = my_string[0].lower()
    
    if my_string.lower().startswith(first_char) and my_string.lower().endswith(first_char):
        return "Same character"
    else:
        return "Different characters"

# Test multiple strings
test_cases = ["Python", "level", "Hello", "racecar", "A"]

for text in test_cases:
    result = check_string_builtin(text)
    print(f"'{text}': {result}")
'Python': Different characters
'level': Different characters
'Hello': Different characters
'racecar': Same character
'A': Same character

Comparison

Method Complexity Best For
Regular Expression High Complex pattern matching
String Indexing Low Simple and efficient
Built-in Methods Medium Readable code

Conclusion

For simple character comparison, string indexing with my_string[0] and my_string[-1] is the most efficient approach. Use regular expressions only when you need complex pattern matching beyond simple character comparison.

Updated on: 2026-03-26T02:49:33+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements