Python 對字串切片及翻轉

Document 對象參考手冊 Python3 實例

給定一個字串,從頭部或尾部截取指定數量的字串,然後將其翻轉拼接。

實例

def rotate(input,d): Lfirst = input[0 : d] Lsecond = input[d :] Rfirst = input[0 : len(input)-d] Rsecond = input[len(input)-d : ] print( "頭部切片翻轉 : ", (Lsecond + Lfirst) ) print( "尾部切片翻轉 : ", (Rsecond + Rfirst) ) if __name__ == "__main__": input = 'zaixian' d=2 # 截取兩個字元 rotate(input,d)

執行以上代碼輸出結果為:

頭部切片翻轉 :  noobRu
尾部切片翻轉 :  obRuno

Document 對象參考手冊 Python3 實例