python字符串的翻转实现的两种方法
发布时间:2024-09-04 19:51:51

实现python字符串翻转的两种方法
方法1:使用切片
str1="helloworld!" print(str1[::-1])
方法二:利用reduce函数实现
fromfunctoolsimportreduce str1="helloworld!" print(reduce(lambdax,y:y+x,str1))
补充:判断字符串是否为回文字符串
str1="123455"
deffun(string):
print("%s"%string==string[::-1]and"YES"or"NO")
if__name__='__main__':
fun(str1)
下一篇 Python如何定制日志输出格式
