50% found this document useful (2 votes)
2K views

6.7 LAB Palindrome

This document provides an example of a program that checks if a word or phrase is a palindrome by reading it both forward and backward while ignoring spaces. The program takes a string as input, removes spaces and converts it to lowercase before using a while loop to check if each character at the start matches the character at the end as it iterates through half the length of the string. It then prints whether the input is or is not a palindrome based on the outcome of this check.

Uploaded by

CHRIS D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
2K views

6.7 LAB Palindrome

This document provides an example of a program that checks if a word or phrase is a palindrome by reading it both forward and backward while ignoring spaces. The program takes a string as input, removes spaces and converts it to lowercase before using a while loop to check if each character at the start matches the character at the end as it iterates through half the length of the string. It then prints whether the input is or is not a palindrome based on the outcome of this check.

Uploaded by

CHRIS D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

6.

7 LAB Palindrome
A palindrome is a word or a phrase that is the same when read both forward and
backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a
program whose input is a word or phrase, and that outputs whether the input is a
palindrome.
Ex: If the input is:
bob
the output is:
bob is a palindrome
Ex: If the input is:
bobby
the output is:
bobby is not a palindrome
Hint: Start by removing spaces. Then check if a string is equivalent to it's reverse.

string=input()
str = string.replace(" ", "").lower()
i=0
ispalindrome = True
while i < len(str)/2:
if str[i] != str[-1-i]:
ispalindrome = False
break
i+=1
if ispalindrome:
print (string+ " is a palindrome")
else:
print (string+ " is not a palindrome")

You might also like