当前位置:主页 > 软件编程 > Python代码 >

python字符串反转的四种方法详解

时间:2021-08-17 07:34:41 | 栏目:Python代码 | 点击:

这篇文章主要介绍了python字符串反转的四种详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、用reduce函数方法

book = 'Python程序设计'
result = reduce(lambda x,y:y+x,book)
print(result)

2、字符串切割

book = 'Python程序设计'
print(book[::-1])

3、用reversed方法,把字符串变成列表反转后拼接

result = reversed(list(book))
print(''.join(result))

4、for循环

book = 'Python程序设计'
lenbook = len(book) - 1
result = ''
for index,value in enumerate(book):
 result += book[lenbook - index]
print(result)

您可能感兴趣的文章:

相关文章