Time for another Python challenge. This time it’s the palindrome challenge. What is a palindrome? A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward. Some examples are level, radar, stats.

The goal is to take a string the user inputs, reverse the string and see if it’s identical backward as forward. I will divide my code into two functions:

  • get_string
  • check_palindrome

The first function simply takes the string that is input. The second function checks if it’s a palindrome and prints the result.

As always, let’s first start with a docstring:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"""Program to check if string is a palindrome"""
"""Program to check if string is a palindrome"""
"""Program to check if string is a palindrome"""

Then we create a function to get the string from the user. This code should look familiar if you went through the divisors challenge.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def get_string():
"""Get a string from user to be used in function to check for palindrome"""
# Get string from user with input() and return result
user_string = input("Please enter a string to check if it's a palindrome: ")
return user_string
def get_string(): """Get a string from user to be used in function to check for palindrome""" # Get string from user with input() and return result user_string = input("Please enter a string to check if it's a palindrome: ") return user_string
def get_string():
    """Get a string from user to be used in function to check for palindrome"""
    # Get string from user with input() and return result 
    user_string = input("Please enter a string to check if it's a palindrome: ")
    return user_string

Now for the more interesting part, to check if a string is a palindrome. To do that, we need to reverse the string. How can we do that? The easiest way is probably to use slicing. Let’s give an example using the REPL:

First we create a string, the word radar:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
daniel@devasc:~/DevAsc$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> word = "radar"
>>>
daniel@devasc:~/DevAsc$ python3 Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> word = "radar" >>>
daniel@devasc:~/DevAsc$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> word = "radar"
>>> 

If we want to access a specific letter of this word, we can use access it by index. For example, to access the first letter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> word[0]
'r'
>>> word[0] 'r'
>>> word[0]
'r'

Notice that counting starts from from zero. If we want to access the last letter, it is accessible at

-1
-1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> word[-1]
'r'
>>> word[-1] 'r'
>>> word[-1]
'r'

We can also access a range of letters, a

slice
slice, by specifying a
start
start and
stop
stop with a colon in between. For example, the three first ones, with a range from
0
0 to
3
3, because the
stop
stop index is not inclusive:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> word[0:3]
'rad'
>>> word[0:3] 'rad'
>>> word[0:3]
'rad'

It’s also possible to use a

stepping
stepping. What if we want to access the entire string but only every other letter? We can do that by specifying
0:6:2
0:6:2. Start at index
0
0, stop at
6
6 not inclusive, with a
stepping
stepping of
2
2.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> word[0:6:2]
'rdr'
>>> word[0:6:2] 'rdr'
>>> word[0:6:2]
'rdr'

The drawback here though is that this is not a generic approach since we have specified the

stop
stop manually. There are defaults when using slicing that we can leverage to write this more efficiently. The defaults, when using a positive
stepping
stepping, are these:

  • Start at index 0
  • End where the string ends
  • Stepping of 1

This means that we can access the slice with

::2
::2, where we are using the default values for
start
start and
stop
stop, as indicated by the colon, meaning to start at
0
0 and end where the string ends.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> word[::2]
'rdr'
>>> word[::2] 'rdr'
>>> word[::2]
'rdr'

Awesome! So how do we then find the reverse of a string? The trick is to use a negative

stepping
stepping. This is where things can get a bit confusing. When using a negative
stepping
stepping, the defaults for
start
start and
stop
stop change. The defaults are then these:

  • Start at the end of the string
  • Stop at the beginning of the string

This means that we can reverse a string by using

::-1
::-1 where the first colon means to start at the end of string, go to the start of the string, with a stepping of
-1
-1, that is, go one step backward each time.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> word[::-1]
'radar'
>>> word[::-1] 'radar'
>>> word[::-1]
'radar'

Great. Now let’s write this as a function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def check_palindrome(user_string):
"""Reverse string from user and check if palindrome"""
user_string_reverse = user_string[::-1]
if user_string_reverse == user_string:
print("It's a palindrome!")
else:
print("It's NOT a palindrome!")
def check_palindrome(user_string): """Reverse string from user and check if palindrome""" user_string_reverse = user_string[::-1] if user_string_reverse == user_string: print("It's a palindrome!") else: print("It's NOT a palindrome!")
def check_palindrome(user_string):
    """Reverse string from user and check if palindrome"""
    user_string_reverse = user_string[::-1]
    if user_string_reverse == user_string:
        print("It's a palindrome!")
    else:
        print("It's NOT a palindrome!")

We store the

user_string
user_string in reverse in
user_string_reverse
user_string_reverse. Then we check if they are equal. If they are, it’s a palindrome!

We must, of course, run the functions:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if __name__ == "__main__":
user_string = get_string()
check_palindrome(user_string)
if __name__ == "__main__": user_string = get_string() check_palindrome(user_string)
if __name__ == "__main__":
    user_string = get_string()
    check_palindrome(user_string)

When we run the code, this is what it looks like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
l@devasc:~/DevAsc$ python3 palindrome.py
Please enter a string to check if it's a palindrome: radar
It's a palindrome!
daniel@devasc:~/DevAsc$ python3 palindrome.py
Please enter a string to check if it's a palindrome: boat
It's NOT a palindrome!
daniel@devasc:~/DevAsc$
l@devasc:~/DevAsc$ python3 palindrome.py Please enter a string to check if it's a palindrome: radar It's a palindrome! daniel@devasc:~/DevAsc$ python3 palindrome.py Please enter a string to check if it's a palindrome: boat It's NOT a palindrome! daniel@devasc:~/DevAsc$
l@devasc:~/DevAsc$ python3 palindrome.py 
Please enter a string to check if it's a palindrome: radar
It's a palindrome!
daniel@devasc:~/DevAsc$ python3 palindrome.py 
Please enter a string to check if it's a palindrome: boat
It's NOT a palindrome!
daniel@devasc:~/DevAsc$ 

Finally, here’s the entire code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"""Program to check if string is a palindrome"""
def get_string():
"""Get a string from user to be used in function to check for palindrome"""
# Get string from user with input() and return result
user_string = input("Please enter a string to check if it's a palindrome: ")
return user_string
def check_palindrome(user_string):
"""Reverse string from user and check if palindrome"""
user_string_reverse = user_string[::-1]
if user_string_reverse == user_string:
print("It's a palindrome!")
else:
print("It's NOT a palindrome!")
if __name__ == "__main__":
user_string = get_string()
check_palindrome(user_string)
"""Program to check if string is a palindrome""" def get_string(): """Get a string from user to be used in function to check for palindrome""" # Get string from user with input() and return result user_string = input("Please enter a string to check if it's a palindrome: ") return user_string def check_palindrome(user_string): """Reverse string from user and check if palindrome""" user_string_reverse = user_string[::-1] if user_string_reverse == user_string: print("It's a palindrome!") else: print("It's NOT a palindrome!") if __name__ == "__main__": user_string = get_string() check_palindrome(user_string)
"""Program to check if string is a palindrome"""

def get_string():
    """Get a string from user to be used in function to check for palindrome"""
    # Get string from user with input() and return result 
    user_string = input("Please enter a string to check if it's a palindrome: ")
    return user_string

def check_palindrome(user_string):
    """Reverse string from user and check if palindrome"""
    user_string_reverse = user_string[::-1]
    if user_string_reverse == user_string:
        print("It's a palindrome!")
    else:
        print("It's NOT a palindrome!")

if __name__ == "__main__":
    user_string = get_string()
    check_palindrome(user_string)

It is available via Github as well.

Bonus: There’s another way of solving this challenge, with a For loop, although slicing is the preferred method. It looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
daniel@devasc:~/DevAsc/Python-challenges$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> word = "radar"
>>> reverse = ""
>>> for i in range(len(word)):
... reverse += word[len(word)-1-i]
...
>>> print(reverse)
radar
>>>
daniel@devasc:~/DevAsc/Python-challenges$ python3 Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> word = "radar" >>> reverse = "" >>> for i in range(len(word)): ... reverse += word[len(word)-1-i] ... >>> print(reverse) radar >>>
daniel@devasc:~/DevAsc/Python-challenges$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> word = "radar"
>>> reverse = ""
>>> for i in range(len(word)):
...     reverse += word[len(word)-1-i]
... 
>>> print(reverse)
radar
>>> 

We have the string radar stored in

word
word. We also have an empty string called
reverse
reverse. We can iterate through
word
word. To do this, we use a for loop that loops through
range(len(word))
range(len(word)), that is, the length of the word.

We then add letters to

reverse
reverse, starting at the end. Why do we have
len(word)-1
len(word)-1? Because of the zero index. Otherwise we would try to access an index that is not available. Why
-i
-i at the end? So that we move towards the start of the string. Note that the iterator is
0
0 when it starts, then it’s
1
1,
2
2,
3
3, and so on.

Happy coding!

DevAsc – Python Palindrome Challenge
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *