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

方法一:利用切片
str1 = "hello world!" print(str1[::-1])
方法二:利用reduce函數(shù)實(shí)現(xiàn)
from functools import reduce str1 = "hello world!" print(reduce(lambda x, y : y+x, str1))
補(bǔ)充:判斷字符串是不是回文串
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)實(shí)現(xiàn)的兩種方法
文章地址:http://m.5511xx.com/article/djegcph.html


咨詢
建站咨詢
