Easy Tutorial
❮ Python String Python Att Time Tzset Html ❯

String Slicing and Reversing in Python

Python3 Examples

Given a string, slice a specified number of characters from the head or tail, and then concatenate them in reverse order.

Example

def rotate(input, d): 

    Lfirst = input[0 : d] 
    Lsecond = input[d :] 
    Rfirst = input[0 : len(input)-d] 
    Rsecond = input[len(input)-d : ] 

    print("Head Slicing and Reversing: ", (Lsecond + Lfirst))
    print("Tail Slicing and Reversing: ", (Rsecond + Rfirst))

if __name__ == "__main__": 
    input = 'tutorialpro'
    d = 2  # Slice two characters
    rotate(input, d)

Executing the above code produces the following output:

Head Slicing and Reversing:  torialprotu
Tail Slicing and Reversing:  rotutorialp

Python3 Examples

❮ Python String Python Att Time Tzset Html ❯