Python Forum
print string in reverse
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print string in reverse
#1
fruit = 'banana'
index = 0
while index < len(fruit):
    letter = fruit[index]
    print(letter, end='')
    index = index + 1
I tried to reverse the string by changing 3rd line to letter = fruit[-index], but it didn't work Snooty
Reply
#2
>>> s = 'Welcome to the forum!'
>>> s[::-1]
'!murof eht ot emocleW'
>>> 
This is called slicing. The format is [start_index, stop_index, step]. Stop_index is exclusive. In this case, the start and the stop are missing which means we get the whole string. And since the step is negative it counts backward.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Oct-02-2018, 11:49 PM)wavic Wrote:
>>> s = 'Welcome to the forum!'
>>> s[::-1]
'!murof eht ot emocleW'
>>> 
This is called slicing. The format is [start_index, stop_index, step]. Stop_index is exclusive. In this case, the start and the stop are missing which means we get the whole string. And since the step is negative it counts backward.

thanks for the reply and the welcome Heart. but slicing is explained after the exercise. so i solved it like this instead.
fruit = 'banana'
index = len(fruit) - 1
while index >= 0:
    letter = fruit[index]
    print(letter, end='')
    index = index - 1
Reply
#4
This would have been simpler if you can't use slicing yet:

fruit = 'banana'
reverse = ''
for letter in fruit:
    reverse = letter + reverse
print(reverse)
We generally avoid directly indexing into lists/strings/etc if we don't need to.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
(Oct-04-2018, 04:23 PM)gruntfutuk Wrote: This would have been simpler if you can't use slicing yet:

fruit = 'banana'
reverse = ''
for letter in fruit:
    reverse = letter + reverse
print(reverse)
We generally avoid directly indexing into lists/strings/etc if we don't need to.

I don't get it. shouldn't that just go through each letter and print the result. why did it do it in reverse?
Reply
#6
(Oct-04-2018, 06:55 PM)pseudo Wrote: why did it do it in reverse?
because each letter is added at the begining of the result. to see it
fruit = 'banana'
reverse = ''
for letter in fruit:
    reverse = letter + reverse
    print(reverse)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading a text until matched string and print it as a single line cananb 1 2,017 Nov-29-2020, 01:38 PM
Last Post: DPaul
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,541 Feb-27-2020, 09:22 AM
Last Post: buran
  Reverse string sentence with for loop? BillGates 3 6,079 May-02-2017, 04:15 PM
Last Post: wavic
  Print the index of the vowels in a string MeeranRizvi 4 14,670 Dec-29-2016, 02:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020