程序打印出以最后一个字符结尾的所有子字符串,从最短到最长

问题描述 投票:0回答:1

样本输出

Please type in a string: test
t
st
est
test

我一直在尝试使用 i=i-1 的方法,其中方括号包含 [i:i-1]

input_str1 = input("Please type in a string: ")
i =len(input_str1)+1
while True:
  print(input_str1[i:i-1])
  i=i-1
python
1个回答
0
投票

操作

input_str1[i:i-1]
将始终返回空字符串,因为起始索引大于结束索引:

input_str1 = input("Please type in a string: ")
i = len(input_str1)
while i > 0:
    print(input_str1[i-1:])
    i = i - 1
© www.soinside.com 2019 - 2024. All rights reserved.