Practice Python

Beginner Python exercises

19 March 2014

String Lists Solutions

Exercise 6

Ask the user for a string and print out whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)

Sample solution

A sample solution using string reversal

wrd=input("Please enter a word")
wrd=str(wrd)
rvs=wrd[::-1]
print(rvs)
if wrd == rvs:
print("This word is a palindrome")
else:
print("This word is not a palindrome")

A sample solution using for loops

def reverse(word):
x = ''
for i in range(len(word)):
x += word[len(word)-1-i]
return x
word = input('give me a word:\n')
x = reverse(word)
if x == word:
print('This is a Palindrome')
else:
print('This is NOT a Palindrome')
view raw stringlists.py hosted with ❤ by GitHub

Enjoying Practice Python?


Explore Yubico
Explore Yubico