Home » Python programs

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?
Submitted by Ankit Rai, on July 28, 2019

Input a string from the user and print its reverse.

Example:

    Input:
    "Ankit"

    Output:
    "tiknA"

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

1) Using the concept of string Slicing: Take string input from the user then use string slicing concept.

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 : Ankit
reverse string : tiknA 

2) Using string concatenation concept: Take string input from the user, simply iterate the string from the last character till the first character and concatenated with previous 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 : Ankit
reverse string : tiknA

3) Make a user-defined function for reversing the string: 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.

# 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 : Ankit
reverse string : tiknA

4) Make a user-defined function for reversing the string: 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.

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 : Ankit
reverse string : tiknA

5) Using reversed() and join() function: 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.

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)

Output

Enter a string : Ankit
reverse string : tiknA





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.