I tried to strip the "Never Odd or Even" to remove the whitespaces but it didn't worked or i was not doing it correctly.
So I tried to split the string into list and then join them such that all the whitespace would be removed.
here is the code :
def is_palindrome(input_string):
new_string = ""
reverse_string = ""
lowerinput = input_string.lower()
splitinginput = lowerinput.split()
for new_string in lowerinput:
if lowerinput[0] == lowerinput[-1]:
new = new_string.join(splitinginput)
reverse_string = new_string[::-1]
if new_string == reverse_string:
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
Sorry i am new and this question could be little naive but i would love to know your feedback..
CodePudding user response:
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
CodePudding user response:
this is my solution
def is_palindrome(input_string):
input_string = input_string.lower()
input_string = "".join(input_string.split())
reverse_string = input_string[::-1]
if input_string == reverse_string:
return True
else:
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True