Reverse A String

Posted on January 2, 2015 by

Program to reverse a string.

Example: Reverse of 'Hello World' is 'dlroW olleH'

Code # Method 1

def reverse(str):
    return str[::-1]

if __name__=="__main__":
    print("Enter a String: ",end="")
    str = input()
    print("Reverse of '",str,"' is '",reverse(str),"'")

Code # Method 2

def reverse(string):
    result = ""
    for letter in xrange(len(string), 0, -1):
        result = result + string[letter-1]
    return result

if __name__=="__main__":
    print("Enter a String: ",end="")
    str = input()
    print("Reverse of '",str,"' is '",reverse(str),"'")

Fork Download

This website is made possible by displaying online advertisements to our visitors.
Please consider supporting by disabling your ad blocker.
loading comments...