Skip to content

Instantly share code, notes, and snippets.

@AnisahTiaraPratiwi
Created March 22, 2021 08:15
Show Gist options
  • Save AnisahTiaraPratiwi/7cb4451ceeed9607e19ffd8756051579 to your computer and use it in GitHub Desktop.
Save AnisahTiaraPratiwi/7cb4451ceeed9607e19ffd8756051579 to your computer and use it in GitHub Desktop.
The is_palindrome function checks if a string is a palindrome. A palindrome is a string that can be equally read from left to right or right to left, omitting blank spaces, and ignoring capitalization. Examples of palindromes are words like kayak and radar, and phrases like "Never Odd or Even". Fill in the blanks in this function to return True …
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for string in input_string.lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if string.replace(" ",""):
new_string = string + new_string
reverse_string = string + reverse_string
# Compare the strings
if new_string[::-1]==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
@Naganona
Copy link

Naganona commented Mar 3, 2022

You can replace line 10 -- if string.replace(" ",""):
with -- if string.strip():
to remove any whitespaces

@moizuddin78
Copy link

##I have tried like this

def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for message in input_string.lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if message.strip():
new_string = message + new_string
reverse_string = reverse_string + message
# Compare the strings
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

@halayi
Copy link

halayi commented May 15, 2022

@moizuddin78 the script worked.

@urslovinglysundar
Copy link

Can you please explain the above code?

@LongTran-GD
Copy link

May be this code will be easier to understand

`def is_palindrome(input_string):
	# We'll create two strings, to compare them
	stadard_string = input_string.lower().replace(" ","")
	if stadard_string==stadard_string[::-1]:
		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`

@Omar-Eses
Copy link

Omar-Eses commented Oct 18, 2022

I have tried it as follows:

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = "".join(input_string).lower().replace(" ", "")
    reverse_string = "".join(input_string[::-1]).lower().replace(" ", "")
    ok = True
    # Traverse through each letter of the input string
    for s in range(len(new_string)):
        
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        if new_string[s] != reverse_string[s-len(reverse_string)]:
            ok = False
            break
    # Compare the strings
    if ok:
        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

@loretsky
Copy link

loretsky commented Nov 6, 2022

def is_palindrome(input_string):
new_string= input_string.replace(" ", "").upper()
reverse_string= new_string[::-1]
if new_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

I erased other codes on this quiz. My brain is not braining on that.

@Alyahmedd
Copy link

def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for letter in input_string.lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if input_string.strip():
new_string = letter.strip() + new_string
reverse_string = reverse_string + letter.strip()
# Compare the strings
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

@nguyen-hang
Copy link

image

@nguyen-hang
Copy link

nguyen-hang commented Mar 18, 2023

def palindrome(s):
s2='' # new string with only alphabet characters
for index, letter in enumerate(s): #enumerate built-in function allows us to loop through strings with pair val index and value
if letter.isalpha() == True: #isalpha method to check if letter is in the alphabet or not
s2 = s2 + s[index]
else: continue
if s2[::-1] == s2 : #reversing by using string slicing
return True
else:
return False

@fabinahian
Copy link

I tried this process:

def is_palindrome(input_string):

    new_string = ""
    reverse_string = ""

    for letter in input_string.lower():

        if letter != " ":

            new_string = new_string + letter
            reverse_string = letter + reverse_string

    if reverse_string == new_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

@Sushil2k4
Copy link

This one worked for me
👇🏻

def is_palindrome(input_string):
    # Two variables are initialized as string date types using empty 
    # quotes: "reverse_string" to hold the "input_string" in reverse
    # order and "new_string" to hold the "input_string" minus the 
    # spaces between words, if any are found.
    new_string = ""
    reverse_string = ""

    # Complete the for loop to iterate through each letter of the
    # "input_string"
    for letter in input_string:

        # The if-statement checks if the "letter" is not a space.
        if letter != " ":

            # If True, add the "letter" to the end of "new_string" and
            # to the front of "reverse_string". If False (if a space
            # is detected), no action is needed. Exit the if-block.
            new_string = new_string + letter.lower()
            reverse_string = letter.lower() + reverse_string

    # Complete the if-statement to compare the "new_string" to the
    # "reverse_string". Remember that Python is case-sensitive when
    # creating the string comparison code. 
    if new_string ==  reverse_string:

        # If True, the "input_string" contains a palindrome.
        return True
		
    # Otherwise, return False.
    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

@Dyprof
Copy link

Dyprof commented Jun 25, 2025

If a valid email is known to have a mix of characters and numbers and then ends with @edibrige.ng, e.g nnebaba123@edubridge.ng.
Write a functon named is_valid_email that accepts an email as an input argument and then check if the email is valid based on the condition above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment