Arie Bregman

Linux And Stuff

Python: Check whether a string is Palindrome or not

A palindrome is a sequence of characters which reads the same backward or forward.

For example dad, mom, kayak, a santa at nasa, anna. Not palindromes: mario, luigi, ice cream.

There is more than one way in python to check whether given string is palindrome or not. Let’s have a look.

Method 1

defiantly one of the shortest ways to do it.

[::-1] taking care of inverting the string and all is left is simply compare between the original to inverted string.

Method 2

Slower than the first method:

Method 3

My least favorite but quite common (especially in other languages)

Q&A

Q: How can I handle case-sensitive palindrome?

A: you can use upper() when comparing the reversed and the original strings.

5 Comments

  1. Very nice post. I just stumbled upon your blog and wanted to sayy
    that I’ve really enjoyed surfing around your bloog posts.
    After all I’ll be subscribing to ylur feed and I
    hope you write again very soon!

  2. Right now it looks like Wordpres is the bedst blogging platfrm available right now.

    (from what I’ve read) Is that what you are using on your blog?

  3. Someone essentially lend a hand to make severely posts I’d state.
    That is the first time I frequented your webgsite page aand so
    far? I surprised with the rewsearch you made to create
    this particular post extraordinary. Fantastic process!

Leave a Reply

Your email address will not be published.

*

© 2017 Arie Bregman

Theme by Anders NorenUp ↑