python - 删除字符时向后打印字符串

标签 python python-3.x

这会打印单词,同时从开头删除字符。

word = "word"

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

所以输出是

word
ord
rd
d

我如何翻转它以便在删除字符的同时向后打印单词?
这样输出将是:

drow
row
ow
w

最佳答案

你可以简单地做:

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

[::-1] 切片反转字符串。缺点是双切片都创建了一个新的 str 对象。

关于python - 删除字符时向后打印字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58198792/

相关文章:

python - Numpy 数组的 keras 列表不是预期的大小模型

Python字典理解示例

python - socket.gaierror : [Errno -2] Name or service not known | Python

python - 在 python 3 中使用 .bmp 文件

python - 我无法使用 celery 任务

python - qtreewidget 不需要的空白(额外的不可删除的行)

python - 使用 pandas df.query() 过滤列中的字符串值列表

python - 根据您拥有的日期和年份阅读多个excel

python - 我试图在 python 上解决以下问题,我得到了正确的输出,但它们仍然显示错误

python - 在 python 中安装 Google OR-Tools 时出错