Python Program to Check Palindrome String
You can check if a string is a palindrome in Python by comparing it to its reverse using
if s == s[::-1]: where s is the input string.Examples
Inputmadam
Outputmadam is a palindrome
Inputhello
Outputhello is not a palindrome
Inputa
Outputa is a palindrome
How to Think About It
To check if a word is a palindrome, think about reading it forwards and backwards. If both are exactly the same, then the word is a palindrome. So, reverse the string and compare it with the original string.
Algorithm
1
Get the input string from the user2
Reverse the input string3
Compare the original string with the reversed string4
If both are equal, print that it is a palindrome5
Otherwise, print that it is not a palindromeCode
python
s = input('Enter a string: ') if s == s[::-1]: print(f'{s} is a palindrome') else: print(f'{s} is not a palindrome')
Output
Enter a string: madam
madam is a palindrome
Dry Run
Let's trace the input 'madam' through the code
1
Input string
s = 'madam'
2
Reverse string
s[::-1] = 'madam'
3
Compare original and reversed
'madam' == 'madam' is True
4
Print result
Print 'madam is a palindrome'
| Original String | Reversed String | Are Equal? |
|---|---|---|
| madam | madam | True |
Why This Works
Step 1: Reverse the string
Using s[::-1] creates a reversed copy of the string s.
Step 2: Compare strings
If the original string and reversed string are exactly the same, it means the string reads the same forwards and backwards.
Step 3: Print result
Based on the comparison, print whether the string is a palindrome or not.
Alternative Approaches
Using a loop to compare characters
python
s = input('Enter a string: ') flag = True for i in range(len(s)//2): if s[i] != s[-(i+1)]: flag = False break if flag: print(f'{s} is a palindrome') else: print(f'{s} is not a palindrome')
This method manually compares characters from start and end, useful for understanding but longer code.
Using recursion
python
def is_palindrome(s): if len(s) <= 1: return True if s[0] != s[-1]: return False return is_palindrome(s[1:-1]) s = input('Enter a string: ') if is_palindrome(s): print(f'{s} is a palindrome') else: print(f'{s} is not a palindrome')
Recursive approach is elegant but less efficient for very long strings.
Complexity: O(n) time, O(n) space
Time Complexity
Reversing the string takes O(n) time where n is the string length, and comparing two strings also takes O(n). Overall O(n).
Space Complexity
Reversing the string creates a new string of length n, so space complexity is O(n).
Which Approach is Fastest?
Using slicing s[::-1] is concise and fast for most cases. The loop method uses O(1) extra space but more code. Recursion adds overhead and is less efficient.
| Approach | Time | Space | Best For |
|---|---|---|---|
| String slicing (s[::-1]) | O(n) | O(n) | Simple and fast for most cases |
| Loop character comparison | O(n) | O(1) | Memory efficient, good for large strings |
| Recursion | O(n) | O(n) | Elegant but less efficient for long strings |
Use
s[::-1] to quickly reverse a string in Python.Forgetting that string comparison is case-sensitive, so 'Madam' and 'madam' are different.