Question

Write a python program that contains a hardcoded string and checks whether it is a palindrome...

Write a python program that contains a hardcoded string and checks whether it is a palindrome (the same forwards as backwards). If it is then display “It is a palindrome”, otherwise display “It is not a palindrome”. A hardcoded string is simply a string defined in your program, for example: my_string = “Hello” Some sample palindromes to use are: A car, a man, a maraca Desserts, I stressed Madam, I’m Adam You will need to strip out any punctuation such as commas and apostrophes. Luckily, there’s a function that will allow us to replace (hint) a particular substring with anything (including the empty string – “”). Try having a list of punctuation characters, or a string of them all, and checking each character in the original string against that list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here is the updated code in python. all lines are commented so that you can understand and follow the code

let me know for any questions or changes

# string to check
my_string='majA car, a man a maraca jam'
# list of common punctuations
punctuation_list=['!','@','#','\'','-','_','\"','^','%','*','(',')','+','+','{','}',' ',',']
# will store the punctuations stripped out string
cleaned_string=''
# will hold the reverse string
reverse_string=''
# convert all the letter to lower case
my_string_lower_case=my_string.lower()
# iterate over each letter in the string
for letter in my_string_lower_case:
    #if the letter is in the punctuation list we dont consider that letter
   
if letter in punctuation_list:continue
   
# build the cleaned string
   
cleaned_string+=letter
    # build the reverse string
   
reverse_string=letter+reverse_string
# print the cleaned string and reverse string
print('Cleaned :',cleaned_string)
print('Reversed:',reverse_string)
# compare both the cleaned and reverse string are same and print the message
if cleaned_string==reverse_string:
    print('It is a palindrome')
else:
    print('It is not a palindrome')

Add a comment
Know the answer?
Add Answer to:
Write a python program that contains a hardcoded string and checks whether it is a palindrome...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++, write a stack-based application, assignment7.cpp, which can be used to recognize palindromes (strings spelled...

    In C++, write a stack-based application, assignment7.cpp, which can be used to recognize palindromes (strings spelled identically both backward and forwards). Your program should ignore spaces and punctuation, as well as alpha cases; so, for instance, the string “Madam I’m Adam” counts as a palindrome. You must supply your own push and pop operations. For this exercise (alone) you can use global variables for the stack and the stack pointer. Be sure that your program does not let the user...

  • #19 with the following instructions 19. A palindrome is a string that can be read backward...

    #19 with the following instructions 19. A palindrome is a string that can be read backward and forward with the same result. For example, the following is a palindrome: Able was I ere I saw Elba. unction to test if a string is a palindrome using a stack. You can push characters in the stack one by one. When you reach the en string, you can pop the characters and form a new string. If the two strings are exactly...

  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

  • Write a program palind.c that reads a message, then checks whether it's palindrome (the letters in...

    Write a program palind.c that reads a message, then checks whether it's palindrome (the letters in the message are the same from left to right as from right to left): Enter a message He lived as a devil, eh? Palindrome Enter a message Madam, I am Adam Not a palindrome Ignore all characters that aren't letters. Use integer variables to keep track of positions in the array. Revise the program in Part I to use a pointer instead of an...

  • For this assignment, you will create a program that tests a string to see if it...

    For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is...

  • Write a function palcheck (char word II) which takes a C-style string word (or a C++...

    Write a function palcheck (char word II) which takes a C-style string word (or a C++ string word if you wish) and returns or false depending on whether or not the string is a palindrome. (A palindrome is a string which reads the same backwards and forwards. Some classic example of palindromes are radar, racecar, toot, deed, bib, civic, redder and madam.) Use your function from part (a) in a complete C++ program which, for each small letter 'a' through...

  • python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as...

    python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as argument and returns True if that string (ignoring spaces) is a palindrome. A palindrome is a word or phrase that spells the same thing forwards as backwards (ignoring spaces). Your program should use a recursive process to determine the result, by comparing the first and last letters of the string, and if necessary, calling ispalindrome() on the rest of the string. Sample run: print(ispalindrome('never...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • Concepts: Pointers and pointer arrays Write a program to read in a phrase from the keyboard,...

    Concepts: Pointers and pointer arrays Write a program to read in a phrase from the keyboard, re-display the text, and display a message on the following line giving the text's status as a palindrome. Do not use any arrays or subscripting in this program, except for arrays of pointers. A palindrome is a word or phrase which is the same backwards as forwards; for example "civic", "toot", and "radar" are palindrome words; each word is spelled the same left to...

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT