https://www.php.cn/python-tutorials.html?p=91
Python如何把字符串倒序?下面给大家介绍三种方法:
方法1
使用字符串的分片截图功能
1 2 3 | str_2 = '我的世界因为有你才会美'
#字符串分片截图功能,从尾到头截图,步长为-1即倒序截取
print (str_2[::-1])
|
方法2
使用列表的reverse()函数
相关推荐:《Python视频教程》
1 2 3 4 5 6 | str_1 = 'abccdef'
#将字符串转换为list列表
lst = list(str_1)
#对列表进行反转操作,reverse()返回为None
lst.reverse()
print ( '' .join(lst))
|
方法3
使用字符串的倒序遍历
1 2 3 4 5 6 7 | str_3 = '如果人人都献出一点爱,世界将变成美好的人间'
i = len(str_3) - 1
str_list = []
while (i >= 0):
str_list.append(str_3[i])
i = i - 1
print ( '' .join(str_list))
|
以上就是Python如何把字符串倒序的详细内容,更多请关注php中文网其它相关文章!
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。