新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(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) 網(wǎng)頁(yè)題目:創(chuàng)新互聯(lián)Python教程:python字符串的翻轉(zhuǎn)實(shí)現(xiàn)的兩種方法
網(wǎng)址分享:http://www.dlmjj.cn/article/djegcph.html


咨詢(xún)
建站咨詢(xún)
