string = str(input("Please type in a string: "))
index = 0
while index < len(string):
print(string[index])
index += -1
我得到一个错误,说(字符串索引超出范围)
字符串在python中有点像列表。您可以使用print(string[::-1])
来反向打印
编辑:
逐字符打印
string = string[::-1]
index=0
while index < len(string):
print(string[index])
index += 1
你得到'Index out of range'错误的原因是你的while条件永远不会为false,因为你的Index几乎总是小于0。
这应该修复:
string = str(input())
index = len(string) - 1; # Start at the end of your string
while index > -1: # Until you have reached the end
print(string[index])
index -= 1 # Equivalent to 'index += -1' but cleaner