码迷,mamicode.com
首页 > 编程语言 > 详细

Python 字符串反转

时间:2016-12-19 08:13:26      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:eve   lis   reduce   转化   方法   ever   int   字符串反转   列表   

方法一: 切片的方法

a = "hello"
b = len(a)
i = 1
c = ""
while i<=b:
d = a[b-i]
c += d
i+=1
print c

 方法二:  将字符串转化成列表   reverse()方法将列表反转(没有返回值)  将列表再次拼接成一个字符串

a = "hello"
b = list(a)
b.reverse()
c = "".join(b)
print c

方法三:  切片的方式

a = "hello"
b = a[::-1]

方法四:reduce

a = "hello"
b = reduce(lambda x,y:y+x,a)
print b

  

Python 字符串反转

标签:eve   lis   reduce   转化   方法   ever   int   字符串反转   列表   

原文地址:http://www.cnblogs.com/weilsppython/p/6196024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!