string1 = "abcde"
list1 = list(string1) #将字符串转为数组
list1.reverse() # 使用reverse()方法,使列表元素顺序反转
print(list1)
string2 = "".join(list1) # 将反转后的列表元素拼接成一个新字符串
print(string2)
通过list(str), 使字符串变为列表;
通过list.reverse(), 使列表反转;
通过"".join(list), 使列表变为字符串。
/usr/local/bin/python3.7 /Users/zhouxl/Documents/python小甲鱼视频/小甲鱼py/change.py
['e', 'd', 'c', 'b', 'a']
edcba
Process finished with exit code 0
联系客服