Python String Slicing and Reversal

Document 对象参考手册Python3 Examples

Given a string, extract a specified number of characters from the head or tail, then reverse and concatenate it.

Examples

def rotate(input,d): Lfirst = input[0 : d] Lsecond = input[d :] Rfirst = input[0 : len(input)-d] Rsecond = input[len(input)-d : ] print( "Head slice reversal:", (Lsecond + Lfirst) ) print( "Tail slice reversal:", (Rsecond + Rfirst) ) if __name__ == "__main__": input = 'Example' d=2 # Extract two characters rotate(input,d)

Executing the above code produces the following output:

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

Document 对象参考手册Python3 Examples

Other Extensions