多语言展示
当前在线:832今日阅读:167今日分享:16

python中字符串如何反转?

怎样在 python 中将字符串“hello word”反转打印呢?介绍几种方法供大家参考。
方法/步骤
1

切片法

2

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

3

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

方法/步骤2
1

for循环输出

2

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

推荐信息