Creating substrings with a nested loop

Hi everyone: I’m looking at this case of nested loops.

Write a programme that reads a word and prints all substrings, sorted by length. For example, if the user provides the input “rum”, the programme prints:
r
u
m
ru
um
rum

I have this:
##
# Display all substrings of a word, sorted by length.
#

# Read the word from the user.
word = input("Enter a word: ")

# Generate and display all substrings ordered by increasing length.
for length in range(1, len(word) + 1) :
   for pos in range(0, len(word) - length + 1) :
      print(word[pos : pos + length])

I print everything, but don’t know what the numbers after the second A are referring to or why the formula in the nested loop looks so cryptic:

word = input("Enter a word: ")

# Generate and display all substrings ordered by increasing length.
for length in range(1, len(word) + 1) :
   for pos in range(0, len(word) - length + 1) :
      print(len(word) - length + 1)
      print(length + 1)
      print(word[pos : pos + length])

Enter a word: Ana
3
2
A
3
2
n
3
2
a
2
3
An
2
3
na
1
4
Ana

Thanks!!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.