Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

[Python] - A very simple Palindrome checker

python palindrome checker lkp

  • Please log in to reply
10 replies to this topic

#1 Sundance

Sundance

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 572 posts
  • Programming Language:C, Java, PHP, Python, JavaScript, Perl, PL/SQL, Transact-SQL, Bash, Others

Posted 18 November 2013 - 12:29 PM

This is a very simple Palindrome checker in Python I just wrote it because I had never wrote one before, it was a lot simpler than I thought it would be (luxury of Python).

 

What is a Palindrome?

A Palindrome is a word that is the same when it is reversed for an example if I said "Cat" is a palindrome it is not because if you reverse it, it spells taC however if we have something like "Dad" in reverse it still spells "daD" so this is a palindrome.

print "What is the word you would like to check?" # Asks the user what word they would like to check.
x = raw_input("> ") # User inputs the word.
y = x[::-1] # reverses the string.
if x.lower() == y.lower(): # checks if the strings match, this way it handles case sensitivity.
	print x,"is a palindrome!" # prints it is a palindrome
else: # and if it's not a palindrome
	print x,"is not a palindrome!" # it will print it is not a palindrome!

  • 1

Please read the

FaQ & Guidelines


#2 Anubis

Anubis

    CC Newcomer

  • Member
  • PipPip
  • 19 posts
  • Location:Croatia
  • Programming Language:Python
  • Learning:C++, Visual Basic .NET

Posted 18 November 2013 - 02:48 PM

Looks nice, we did this last year in class, but we used a "for" loop to reverse the string. I didn't know it could be done in such a simple way. Could you explain me how that third line works? Why is it written that way?
  • 0

#3 Sundance

Sundance

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 572 posts
  • Programming Language:C, Java, PHP, Python, JavaScript, Perl, PL/SQL, Transact-SQL, Bash, Others

Posted 18 November 2013 - 03:35 PM

Why is it a variable?

It's a variable just for formatting purposes, makes it much easier for people to learn from, I like to code all my stuff and then comment on it so anyone can learn from it, I emphasize on readability of code.

Why is it [::-1] ?

It gets the entire string and then prints it from reverse, I can't explain 'how' it does that as it's a part of Python but I can explain 'why' if that makes sense?


  • 0

Please read the

FaQ & Guidelines


#4 PBJ

PBJ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 176 posts
  • Programming Language:C, Java, C++, C#, Python, JavaScript, Transact-SQL, Assembly, Bash

Posted 18 November 2013 - 05:51 PM

@OP

This is a nice little trick/exercise, as a result I attempted it in Java, and would be interested to see your one line solution. Here is one that I wrote, however; it is written in Java (however, should easily be translatable)

return isPalindrome = new StringBuilder(stringHere).reverse().toString().equals(stringHere);

Look forward to your response.


Edited by PBJ, 18 November 2013 - 05:51 PM.

  • 1

#5 0xFACEB004

0xFACEB004

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 625 posts
  • Location:Chicago
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Others
  • Learning:Assembly, Others

Posted 18 November 2013 - 07:17 PM

@OP

This is a nice little trick/exercise, as a result I attempted it in Java, and would be interested to see your one line solution. Here is one that I wrote, however; it is written in Java (however, should easily be translatable)

return isPalindrome = new StringBuilder(stringHere).reverse().toString().equals(stringHere);

Look forward to your response.

 

Your one line is very close, but one minor change is needed for it to work exactly the same way, in case some one decides to try it out:

return isPalindrome = new StringBuilder(stringHere).reverse().toString().equalsIgnoreCase(stringHere);

otherwise it will return false for a word like "Dad" because of capitalization, as I am sure you already know :D


Edited by KJGino, 18 November 2013 - 07:18 PM.

  • 1

                                                                                                                                                                            FACEB00K Likes this.


#6 Anubis

Anubis

    CC Newcomer

  • Member
  • PipPip
  • 19 posts
  • Location:Croatia
  • Programming Language:Python
  • Learning:C++, Visual Basic .NET

Posted 18 November 2013 - 10:56 PM

Why is it a variable?
It's a variable just for formatting purposes, makes it much easier for people to learn from, I like to code all my stuff and then comment on it so anyone can learn from it, I emphasize on readability of code.
Why is it [::-1] ?
It gets the entire string and then prints it from reverse, I can't explain 'how' it does that as it's a part of Python but I can explain 'why' if that makes sense?

I understand why it's a variable, but could you explain 'why' then? What would happen if you wrote -2 or any other number? I don't have access to a PC right now, otherwise I would try it out myself.

Edited by Anubis, 18 November 2013 - 10:58 PM.

  • 0

#7 0xFACEB004

0xFACEB004

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 625 posts
  • Location:Chicago
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Others
  • Learning:Assembly, Others

Posted 19 November 2013 - 12:06 AM

I understand why it's a variable, but could you explain 'why' then? What would happen if you wrote -2 or any other number? I don't have access to a PC right now, otherwise I would try it out myself.

 

This is one of the few things that I actually remember from Python. It is called Extended Slices. The syntax for it is: [start:end:step]

 

Start is the first character position in the string you are working with, so if you had [1::] it would start with the char at the index 1 position, and since no end point is defined, it would go to the end of the string.

 

If you had [1:4] - it would start with the position 1 and go to the 4th position (but NOT include the 4th position, so in essence 1 through 3).

 

If you include a step, then you could do something like this: [1:4:2] would output every other char in the string, from position 1 to position 4, but not including the char at position 4.

 

To explain what LKP did - she left the beginning and end undefined so that the whole string would be used, and since the step was -1, it iterated through the string in reverse. So, [::-1] would give you the output of the whole string in reverse.

 

See here for more detailed information on Extended Slices.


  • 2

                                                                                                                                                                            FACEB00K Likes this.


#8 Sundance

Sundance

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 572 posts
  • Programming Language:C, Java, PHP, Python, JavaScript, Perl, PL/SQL, Transact-SQL, Bash, Others

Posted 19 November 2013 - 01:01 AM

This is one of the few things that I actually remember from Python. It is called Extended Slices. The syntax for it is: [start:end:step]

 

Start is the first character position in the string you are working with, so if you had [1::] it would start with the char at the index 1 position, and since no end point is defined, it would go to the end of the string.

 

If you had [1:4] - it would start with the position 1 and go to the 4th position (but NOT include the 4th position, so in essence 1 through 3).

 

If you include a step, then you could do something like this: [1:4:2] would output every other char in the string, from position 1 to position 4, but not including the char at position 4.

 

To explain what LKP did - she left the beginning and end undefined so that the whole string would be used, and since the step was -1, it iterated through the string in reverse. So, [::-1] would give you the output of the whole string in reverse.

 

See here for more detailed information on Extended Slices.

 

KJGino explained that rather well, thank you however he also forgot his cardinal numbers. So the start of the string would be 0 instead of 1.

 

============================================================================

 

@OP

This is a nice little trick/exercise, as a result I attempted it in Java, and would be interested to see your one line solution. Here is one that I wrote, however; it is written in Java (however, should easily be translatable)

return isPalindrome = new StringBuilder(stringHere).reverse().toString().equals(stringHere);

Look forward to your response.

def PalindromeCheck(var) : return var.lower() == var[::-1].lower()

This is it in a single line, also makes sure the text is lowercase again to combat case sensitivity.


Edited by LKP, 19 November 2013 - 01:07 AM.

  • 2

Please read the

FaQ & Guidelines


#9 0xFACEB004

0xFACEB004

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 625 posts
  • Location:Chicago
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Others
  • Learning:Assembly, Others

Posted 19 November 2013 - 01:38 AM

KJGino explained that rather well, thank you however he also forgot his cardinal numbers. So the start of the string would be 0 instead of 1.

 

 

Thank you LKP.

 

But my string example was hypothetical, so, as you know, the beginning of the slice does not necessarily have to be at the beginning of the string (position 0); it can be anywhere in the string -- which is also why I specified which positions of the string would be outputted regarding my example.


  • 1

                                                                                                                                                                            FACEB00K Likes this.


#10 Anubis

Anubis

    CC Newcomer

  • Member
  • PipPip
  • 19 posts
  • Location:Croatia
  • Programming Language:Python
  • Learning:C++, Visual Basic .NET

Posted 19 November 2013 - 08:07 AM

Thank you LKP and KJGino for the explanation. I just got 2 books on Python today. One is about solving basic problems with Python and the other one is solving advance problems with Python. The authors are from Croatia so I'm guessing the books are only available in Croatian for now. I just found a few pages where Extended Slices are explained.

 

def PalindromeCheck(var) : return var.lower() == var[::-1].lower()

This is it in a single line, also makes sure the text is lowercase again to combat case sensitivity.

I also found this in the book, it's pretty similar, 2 lines of code too.


  • 0

#11 Sundance

Sundance

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 572 posts
  • Programming Language:C, Java, PHP, Python, JavaScript, Perl, PL/SQL, Transact-SQL, Bash, Others

Posted 19 November 2013 - 02:49 PM

Thank you LKP and KJGino for the explanation. I just got 2 books on Python today. One is about solving basic problems with Python and the other one is solving advance problems with Python. The authors are from Croatia so I'm guessing the books are only available in Croatian for now. I just found a few pages where Extended Slices are explained.

 

I also found this in the book, it's pretty similar, 2 lines of code too.

 

Generally code will or can look the same within Python (at least from my experience) hence why learning from others scripts can be easy.


  • 0

Please read the

FaQ & Guidelines






Also tagged with one or more of these keywords: python, palindrome, checker, lkp

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download