Python 字符串的翻转

Python 字符串的翻转,第1张

字符串的翻转 用递归方式实现字符串的翻转
# 用递归方式实现字符串的翻转
s='12345678' # 测试用例
def r(s):
    if s=='':
        return s
    else:
        return r(s[1:])+s[0]
r(s)
直接倒序输出
# 直接倒序输出
s='12345678'
s[::-1]
转换成列表后再输出
# 转换成列表再输出
s='12345678'
l=list(s)
l.reverse()
''.join(l)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/942696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存