Python program to turn around a given string (5 distinct ways)

Turning around a string in Python: Here, we will figure out how to switch a given string utilizing various routes in Python?

Info a string from the client and print its turn around.

Example:

    Input:
    "Kishan"

    Output:
    "nahsiK"

Here, we are executing the program to switch a given string utilizing 5 distinct ways.

1) Using the idea of string Slicing: Take string contribution from the client at that point use string cutting idea.

if __name__ == "__main__" :

    string = input('Enter a string : ')

    # reverse a string using string slicing concept
    rev_string = string[::-1]

    print("reverse string :",rev_string)

Output:

Enter a string : Kishan
reverse string : nahsiK

2) Using string connection idea: Take string contribution from the client, basically emphasize the string from the last character till the principal character and linked with past resultant string.

if __name__ == "__main__" :

    string = input('Enter a string : ')

    length = len(string)
    
    rev_string = ''

    # iteration from the last character till
    # first character and cocatenating them
    for index in range(length-1,-1,-1) :
        rev_string += string[index]

    print("reverse string :",rev_string)

Output:

Enter a string : Kishan
reverse string : nahsiK

3) Make a client characterized work for turning around the string: In this capacity, we basically repeat the string from the last character till the main character and connected with a past resultant string. In last we return the last switch string.

# define a function for reversing the string
def reverseString(string) :
    
    length = len(string)
    
    rev_string = ''

    # iteration from the last character till
    # first character and cocatenating them
    for index in range(length-1,-1,-1) :
        rev_string += string[index]

    return rev_string


# Main() method
if __name__ == "__main__" :

    string = input('Enter a string : ')

    print("reverse string :",reverseString(string))

Output:

Enter a string : Kishan
reverse string : nahsiK

4) Make a client characterized work for turning around the string: In this capacity, we just emphasize the string from the last character till the principal character and linked with past resultant string. In last we return the last invert string.

if __name__ == "__main__" :

    string = input('Enter a string : ')

    # covert string into list of characters
    list_char = list(string)

    # reverse the list
    list_char.reverse()

    # join function return string after concatenating the
    # elements from the list in given order with empty string
    rslt = "".join(list_char)
    
    print("reverse string :",rslt)

Output:

Enter a string : Kishan
reverse string : nahsiK

5) Using turned around() and join() work: Take string contribution from the client at that point pass that string in switched() work. turned around() work returns switched object which is changed over into list utilizing list() work then we are utilizing join() work on this rundown which gives turned around the string.

Output:

Enter a string : Kishan
reverse string : nahsiK

Leave a Comment

error: Alert: Content is protected!!