Friday, July 6, 2012

Reverse a String in Python

open a python script editor and just paste the following code in your new python(.py or .pyw) file -

def reverseString ():
    stringvalue = "String Reverse Function"
    print "\n"
    length = stringvalue.__len__()
    arr = ""
    for r in range(0,length):
        newchar = stringvalue[length -1 - r]
        arr += newchar
    print arr
   
reverseString()


output is-

You can do it same through a single line of command-
print 'String Reverse Function'[::-1]
Output is-

No comments:

Post a Comment