新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python字符串的翻轉(zhuǎn)實現(xiàn)的兩種方法
python字符串的翻轉(zhuǎn)實現(xiàn)的兩種方法

方法一:利用切片
str1 = "hello world!" print(str1[::-1])
方法二:利用reduce函數(shù)實現(xiàn)
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)新互聯(lián)Python教程:python字符串的翻轉(zhuǎn)實現(xiàn)的兩種方法
本文來源:http://fisionsoft.com.cn/article/djegcph.html


咨詢
建站咨詢
