首页 > 文章列表 > python字符串翻转的方法

python字符串翻转的方法

python字符串
127 2022-08-07

在字符串的操作中,翻转也是我们常见的操作之一,本篇就其翻转的三种方法带来介绍。

1、最简单的方法是使用切片操作来实现翻转。

2、可以使用reduce函数来实现翻转。

3、在python3中,reduce函数需要从functools中导入。

实例

#方法1
strl = "hello world"
print(strl[::-1])
#方法2
from functools import reduce
print(reduce(lambda x,y:y+x,strl))

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。