python字符串的翻转实现的两种方法

python字符串的翻转实现的两种方法

方法一:利用切片

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)