Python program to reverse a given string (5 different ways)

Reversing a string in Python: Here, we are going to learn how to reverse a given string using different ways in Python? By Ankit Rai Last updated : February 13, 2024

Problem statement

Input a string from the user, write a Python program to print its reverse using different approaches.

Consider the below example with sample input and output:

Input:
"Ankit"

Output:
"tiknA"

Here, we are implementing the program to reverse a given string using 5 different ways.

Reverse string using slicing in Python

Take string input from the user then use string slicing concept.

Python program to reverse string using slicing

if __name__ == "__main__" :

    string = input('Enter a string : ')

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

    print("reverse string :",rev_string)

The output of the above program is:

Enter a string : Ankit
reverse string : tiknA 

Reverse string using concatenation in Python

Take string input from the user, simply iterate the string from the last character till the first character and concatenated with previous resultant string.

Python program to reverse string using concatenation

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)

The output of the above program is:

Enter a string : Ankit
reverse string : tiknA

Reverse string using user-defined function in Python

In this function, we simply iterate the string from the last character till the first character and concatenated with previous resultant string. In last we return the final reverse string.

Python program to reverse string using user-defined function

# 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))

The output of the above program is:

Enter a string : Ankit
reverse string : tiknA

Reverse string using reverse() and join() methods in Python

In this function, we simply iterate the string from the last character till the first character and concatenated with previous resultant string. In last we return the final reverse string.

Python program to reverse string using reverse() and join() methods

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)

The output of the above program is:

Enter a string : Ankit
reverse string : tiknA

Reverse string using list(), reversed(), and join() methods in Python

Take string input from user then pass that string in reversed() function . reversed() function returns reversed object which is converted into list using list() function then we are using join() function on this list which gives reversed string.

Python program to reverse string using list(), reversed(), and join() methods

if __name__ == "__main__" :

    string = input('Enter a string : ')

    # covert string into list of characters
    list_rev = list(reversed(string))

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

The output of the above program is:

Enter a string : Ankit
reverse string : tiknA

Python String Programs »

To understand the above program, you should have the basic knowledge of the following Python topics:

Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.