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
  • 530,221
  • 131
  • 937
  • 1,214
Meisternoob
  • 171
  • 7
  • 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. –  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
  • 72,016
  • 6
  • 67
  • 89
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
  • 88,541
  • 4
  • 65
  • 105