新聞中心
這里有您想知道的互聯網營銷解決方案
創(chuàng)新互聯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) 分享題目:創(chuàng)新互聯Python教程:python字符串的翻轉實現的兩種方法
文章源于:http://m.5511xx.com/article/djegcph.html


咨詢
建站咨詢
