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

Python (169) 2023-05-21 19:35:35

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)
THE END

发表回复