Learn Python Training from the Best Tutors
Search in
By reversing, the rhs will return the reverse of the string.
>>> s='malayalam'
>>> 'is a palindrome' if s==s[::-1] else 'not a palindrome'
'is a palindrome'
>>>
>>> s=input('enter a string... ')
enter a string... racecar
>>> 'is a palindrome' if s==s[::-1] else 'not a palindrome'
'is a palindrome'
>>> s=input('enter a string... ')
enter a string... a string
>>> 'is a palindrome' if s==s[::-1] else 'not a palindrome'
'not a palindrome'
>>>
_______________________________________________________________________________
Otherwise, you can check each character in the string read from the front against each character read from the back..
>>> s='malayalam'
>>> for i in range(len(s)):
if s[i]!=s[-(i+1)]:
'not a palindrome'
break
else: None
else: 'is a palindrome'
'is a palindrome'
>>> s='rotor'
>>> for i in range(len(s)):
if s[i]!=s[-(i+1)]:
'not a palindrome'
break
else: None
else: 'is a palindrome'
'is a palindrome'
>>> s='qwerty'
>>> for i in range(len(s)):
if s[i]!=s[-(i+1)]:
'not a palindrome'
break
else: None
else: 'is a palindrome
'not a palindrome'
>>>
Related Questions
I want to learn Python. but I cant decide which application to choose.I don't understand if I should be a Developer, testing or something else which is the future and which is more in demand.
is there an best online exam simulator for PCAP 31-02 python exam ?
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Top 5 reasons why you should learn Python
Python is one of the most popular programming languages in the world. It is general-purpose, object oriented, high-level programming language used in a number of programming fields. Python is a great programming language to learn as it will introduce you to the world of programming. If you are from the technical background...
Learn Microsoft Excel
Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...
Why Should you Become an IT Consultant
Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...
What is Applications Engineering all about?
Applications engineering is a hot trend in the current IT market. An applications engineer is responsible for designing and application of technology products relating to various aspects of computing. To accomplish this, he/she has to work collaboratively with the company’s manufacturing, marketing, sales, and customer...
Looking for Python Training classes?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for Python Training Classes are on UrbanPro
The best Tutors for Python Training Classes are on UrbanPro