如题所述
1、有时候我们可能想让字符串倒序输出,下面给出几种方法
方法一:通过索引的方法
[python] view plain copy print?
>>> strA = "abcdegfgijlk"
>>> strA[::-1]
'kljigfgedcba'
方法二:借组列表进行翻转
[python] view plain copy print?
#coding=utf-8
strA = raw_input("请输入需要翻转的字符串:")
order = []
for i in strA:
order.append(i)
order.reverse() #将列表反转
print ''.join(order) #将list转换成字符串
执行结果:
[python] view plain copy print?
请输入需要翻转的字符串:abcdeggsdd
ddsggedcba
方法一:通过索引的方法
[python] view plain copy print?
>>> strA = "abcdegfgijlk"
>>> strA[::-1]
'kljigfgedcba'
方法二:借组列表进行翻转
[python] view plain copy print?
#coding=utf-8
strA = raw_input("请输入需要翻转的字符串:")
order = []
for i in strA:
order.append(i)
order.reverse() #将列表反转
print ''.join(order) #将list转换成字符串
执行结果:
[python] view plain copy print?
请输入需要翻转的字符串:abcdeggsdd
ddsggedcba
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-11-01
s = 'abcdef'
s[::-1]
s[::-1]