创新互联Python教程:python字符串的翻转实现的两种方法

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

方法一:利用切片

str1 = "hello world!"
print(str1[::-1])

方法二:利用reduce函数实现

from functools import reduce
str1 = "hello world!"
print(reduce(lambda  x, y : y+x, str1))

补充:判断字符串是不是回文串

str1 = "123455"
def fun(string):
    print("%s" % string == string[::-1] and "YES" or "NO")
if __name__ == '__main__':
    fun(str1)

文章标题:创新互联Python教程:python字符串的翻转实现的两种方法
网页网址:http://csdahua.cn/article/djegcph.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流