python中字符串如何反转?

作者:烟中隐约闪现 | 创建时间: 2023-08-04
怎样在 python 中将字符串“hello word”反转打印呢?介绍几种方法供大家参考。...
python中字符串如何反转?

操作方法

切片法

>>> 'hello world'[::-1] 'dlrow olleh'

切片的扩展语法。步长为-1, 即字符串的翻转

方法/步骤2

for循环输出

string ='hello world' print ''.join(string[i] for i in range(len(string)-1, -1, -1)) dlrow olleh

点击展开全文

更多推荐