How to check for palindrome using Python logic
A pythonic way to determine if a given value is a palindrome:
str(n) == str(n)[::-1]
Explanation:
- We're checking if the string representation of
n
equals the inverted string representation ofn
- The
[::-1]
slice takes care of inverting the string - After that, we compare for equality using
==
How to check if a string is a palindrome?
Just reverse the string and compare it against the original
string_to_check = input("Enter a string")
if string_to_check == string_to_check[::-1]:
print("This is a palindrome")
else:
print("This is not a palindrome")
Palindrome words check function : python 3
There are a few issues:
- The
y
variable should start at -1, since it should reference the last character. - The incremental change to
y
is not correctly indented, as it should be part of thefor
loop input_string[y]
was a good idea, but you need a separateif
condition for checking whether that character is a non-blank- There is a
return new_string
statement that should not be there, since the finalif
still needs to be executed
I should also note that this template code is very restrictive and is not promoting a pythonic coding style, but here are the corrections for the above issues:
def is_palindrome(input_string):
new_string = ""
reverse_string = ""
y = -1 # must be -1 not 1
for letter in input_string:
if letter != " ":
new_string += letter
if input_string[y] != " ": # separate condition
reverse_string += input_string[y]
y -= 1 # fixed indentation
if new_string.lower() == reverse_string.lower(): # added condition
return True
return False
Again, this is not the most elegant solution, nor is it very efficient.
Is this a correct way to get palindrome? I got the result but m not sure
You can check for palindrome without using loops
- Remove all white spaces in the input string.
- Convert the string to lower case.
- Compare the string with its reverse.
- return the output.
def is_palindrome(input_string):
string = input_string.replace(" ", "").lower()
rev_str = string[::-1]
return string == rev_str
print(is_palindrome("Never Odd or Even"))
print(is_palindrome("abc"))
print(is_palindrome("kayak"))
output:
True
False
True
Function to test if a string is a palindrome
The problem is that the return value of your function is always None
, so the assert is_palindrome(...)
will fail and raise an AssertionError
. Any statement following this (in this case your assert not is_palindrome('house')
) will not be executed.
What you need to do is return an appropriate value. For example:
def is_palindrome(s):
string = s
if (string==string[::-1]):
print("The string IS a palindrome")
return True
else:
print("The string is NOT a palindrome")
return False
Checking if a string is palindrome
It compares the last character with the first and moves inside the string as follows:
hannah
^ ^
check the two letters are equal: (return False if not)
move the index by 1
hannah
^ ^
Do the same check
hannah
^^
right index = left index at this point
return True in this case
Determining if a number is a palindrome
Try this to check if number is palindrome or not
if str(temp)==str(temp)[::-1]:
print("Number is palindrome")
else:
print("Not palindrome")
Related Topics
Problem Running Python from Crontab - "Invalid Python Installation"
Problems Adding Path and Calling External Program from Python
Python Multiprocessing + Subprocess Issues
Set Bash Variable from Python Script
How to Deploy a Perl/Python/Ruby Script Without Installing an Interpreter
Unicodedecodeerror, Invalid Continuation Byte
How Is Python's List Implemented
Python Script Not Working via Cron
Docker.Errors.Dockerexception: Error While Fetching Server API Version
Bring the Current Python Program to Background
How to Open Process Again in Linux Terminal
How to Get Multiline Input from the User
How to Install Writable Shared and User Specific Data Files with Setuptools
What Is the Most Efficient Way to Loop Through Dataframes with Pandas