Python 3 教程 Python 对字符串切片及翻转

timothy · 2022-03-15 13:58:15 · 热度: 13

给定一个字符串,从头部或尾部截取指定数量的字符串,然后将其翻转拼接。

实例

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 = 'Codercto' d=2 # 截取两个字符 rotate(input,d)

执行以上代码输出结果为:

头部切片翻转 :  noobRu
尾部切片翻转 :  obRuno

查看更多 Python 实例

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册