Skip to content

Instantly share code, notes, and snippets.

@mayankdawar
Created February 22, 2020 20:50
Show Gist options
  • Save mayankdawar/e53d3b0b2ac830d5be00064de76255a0 to your computer and use it in GitHub Desktop.
Save mayankdawar/e53d3b0b2ac830d5be00064de76255a0 to your computer and use it in GitHub Desktop.
A palindrome is a phrase that, if reversed, would read the exact same. Write code that checks if p_phrase is a palindrome by reversing it and then checking if the reversed version is equal to the original. Assign the reversed version of p_phrase to the variable r_phrase so that we can check your work.
p_phrase = "was it a car or a cat I saw"
r_phrase = p_phrase[::-1]
@shawonAlam
Copy link

can you explain line no 41(r_phrase = p_phrase[::-1])?
i really need an explanation

@mayankdawar
Copy link
Author

can you explain line no 41(r_phrase = p_phrase[::-1])?
i really need an explanation

I have done string slicing here, [ a: b: c]
here a represents starting index of the given string and b represents the last index of string and c represents the step size.
-1 represents the backward step and as if don't mention any value of a and b it means we are inputting complete string as an input. I have also attached an image with this u can see some examples of slicing
Screenshot (16)

@shawonAlam
Copy link

shawonAlam commented May 15, 2020 via email

@SarahAmiraslani
Copy link

Why can't we use the reverse() method?

@mayankdawar
Copy link
Author

Why can't we use the reverse() method?

reverse() function can only be used in case of lists. It will show an error if we use this for string.

@siddharthnagu
Copy link

Thank you

@IldarValishev
Copy link

IldarValishev commented May 30, 2021

p_phrase = "was it a car or a cat I saw"
phrase = []
for letter in p_phrase:
phrase.append(letter)
phrase.reverse()
r_phrase= ''
r_phrase = "".join(phrase)
print(r_phrase)

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