2

This prints the word while removing characters from the beginning.

word = "word"

length = len(word)
for s in range(0, length):
    print(word[s:])
    s=+1

So the output is

word
ord
rd
d

How do I flip it around so it would print the word backwards while removing characters?
So that the output would be:

drow
row
ow
w
Konrad Rudolph
  • 506,650
  • 124
  • 909
  • 1,183
Meisternoob
  • 171
  • 6
  • 2
    FYI, `s=+1` assigns the value `+1` to `s`, and `s` is overwritten immediately anyway by the next loop iteration. In other words, it doesn't do what you think it does and is in fact superfluous. – deceze Oct 02 '19 at 09:33
  • 1
    See [this discussion](https://stackoverflow.com/questions/931092/reverse-a-string-in-python). Oh and the `s=+1` is not needed. – Justin Ezequiel Oct 02 '19 at 09:34

2 Answers2

6

You can simply do:

for s in range(0, length):
    print(word[::-1][s:])

The [::-1] slice reverses the string. The downside to this is the double slices which both create a new str object.

user2390182
  • 67,685
  • 6
  • 55
  • 77
3

Simply (with single reversed slicing):

word = "word"
for i in range(0, len(word)):
    print(word[-1-i::-1])
  • -1-i - negative starting index, to slice on reversed sequence

The output:

drow
row
ow
w
RomanPerekhrest
  • 75,918
  • 4
  • 51
  • 91