具体代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#字符串反转 def reverse (s): rt = '' for i in range ( len (s) - 1 , - 1 , - 1 ): rt + = s[i] return rt def reverse2 (s): li = list (s) li.reverse() rt = "".join(li) return rt def reverse3 (s): return s[:: - 1 ] def reverse4 (s): return "".join( reversed (s)) from functools import reduce def reverse5 (s): return reduce ( lambda x,y:y + x,s) |
前两种是传统方法,后两种比较新颖
总结
以上所述是小编给大家介绍的python中字符串翻转的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/ListentTome/article/details/81002611