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)

以上就是python字符串的翻转实现的两种方法,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

申请14天超长免费试听资格
获取500G教程资料
姓名
电话
课程
立即申请