0
0
PythonProgramBeginner · 2 min read

Python Program to Check Palindrome Using While Loop

You can check a palindrome in Python using a while loop by comparing characters from the start and end moving towards the center with code like while start < end: if s[start] != s[end]: return False; start += 1; end -= 1.
📋

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, start comparing the first and last letters. If they match, move inward by one letter from both ends and compare again. Keep doing this until you reach the middle. If all pairs match, the word is a palindrome; if any pair doesn't match, it is not.
📐

Algorithm

1
Get the input string from the user.
2
Set two pointers: one at the start and one at the end of the string.
3
While the start pointer is less than the end pointer, compare characters at these pointers.
4
If characters differ, conclude the string is not a palindrome and stop.
5
If characters match, move the start pointer forward and the end pointer backward.
6
If the loop finishes without mismatches, conclude the string is a palindrome.
💻

Code

python
s = input('Enter a string: ')
start = 0
end = len(s) - 1
is_palindrome = True
while start < end:
    if s[start] != s[end]:
        is_palindrome = False
        break
    start += 1
    end -= 1
if is_palindrome:
    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

Initialize pointers

start = 0, end = 4 (length of 'madam' - 1)

2

Compare characters at start and end

s[0] = 'm', s[4] = 'm' (match, continue)

3

Move pointers inward

start = 1, end = 3

4

Compare characters again

s[1] = 'a', s[3] = 'a' (match, continue)

5

Move pointers inward

start = 2, end = 2

6

Pointers meet or cross

Loop ends, no mismatches found

startends[start]s[end]match
04mmyes
13aayes
22ddloop ends
💡

Why This Works

Step 1: Two pointers approach

We use two pointers to compare characters from the start and end of the string moving towards the center.

Step 2: Character comparison

If any pair of characters does not match, the string cannot be a palindrome.

Step 3: Loop termination

If the pointers cross without mismatches, the string is confirmed as a palindrome.

🔄

Alternative Approaches

Using string slicing
python
s = input('Enter a string: ')
if s == s[::-1]:
    print(f'{s} is a palindrome')
else:
    print(f'{s} is not a palindrome')
This is simpler and faster but does not use a while loop as requested.
Using recursion
python
def is_palindrome(s, start, end):
    if start >= end:
        return True
    if s[start] != s[end]:
        return False
    return is_palindrome(s, start + 1, end - 1)
s = input('Enter a string: ')
if is_palindrome(s, 0, len(s) - 1):
    print(f'{s} is a palindrome')
else:
    print(f'{s} is not a palindrome')
Uses recursion instead of a loop, which is elegant but uses more memory.

Complexity: O(n) time, O(1) space

Time Complexity

The program compares characters from both ends once, so it runs in linear time proportional to the string length.

Space Complexity

Only a few variables are used for pointers and flags, so space usage is constant.

Which Approach is Fastest?

String slicing is fastest and simplest but does not use a while loop. The while loop method is efficient and clear for beginners.

ApproachTimeSpaceBest For
While loopO(n)O(1)Learning loops and manual checking
String slicingO(n)O(n)Quick palindrome check with less code
RecursionO(n)O(n)Understanding recursion but uses more memory
💡
Always check characters from both ends moving inward to efficiently detect palindromes.
⚠️
Beginners often forget to move both pointers inward, causing infinite loops.