-
-
Save mayankdawar/e53d3b0b2ac830d5be00064de76255a0 to your computer and use it in GitHub Desktop.
p_phrase = "was it a car or a cat I saw" | |
r_phrase = p_phrase[::-1] |
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
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)
can you explain line no 41(r_phrase = p_phrase[::-1])?
i really need an explanation