Python String Slicing and Reversal
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 尾部切片翻转 : obRunoOther Extensions
Python3 Examples