Created
February 22, 2020 20:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p_phrase = "was it a car or a cat I saw" | |
r_phrase = p_phrase[::-1] |
shawonAlam
commented
May 15, 2020
via email
Got it. Thanks.
…On Thu, May 14, 2020, 5:15 PM Mayank Dawar ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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
[image: Screenshot (16)]
<https://user-images.githubusercontent.com/41851148/81927715-9fcef500-95d3-11ea-9ec4-40e1a1416113.png>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/e53d3b0b2ac830d5be00064de76255a0#gistcomment-3303845>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHMIYOWRCK6AQRLPNGIKKFDRRPHFBANCNFSM4M76PEOA>
.
Why can't we use the reverse() method?
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.
Thank you
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