Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
I want to check for a palindrome with Python. The code I have is very for-loop intensive.

And it looks to be the error when going from C to Python is trying to implement C logic using Python, which makes things run slowly, and it's just not making the most of the language.

I've executed this in C, but I need to do it in Python. Kindly guide!

1 Answer

0 votes
by (108k points)

Kindly refer to the below code that will determine if a given value is a palindrome:

str(n) == str(n)[::-1]

Explanation:

  • Here n is the string value.
  • The [::-1] slice takes care of reversing the string
  • After that, we compare for equality using ==

Want to become a Python Developer? Check out this insightful Python Certification course. 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 9, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Feb 11, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Aug 26, 2019 in Python by Sammy (47.6k points)

Browse Categories

...