quest4tech.net

 

Reversing a String in Python

Two examples of reversing a string in Python: one using the the slicing syntax and the other using the 'for' structure.

 
def reverseString(s):
      return s[::-1]
 
print reverseString("Simile and let live.")


def reverseString(chars):
      reverseChars = ""
      lengthStr = len(chars)
      for j in range(lengthStr):
            reverseChars += chars[lengthStr - 1 - j]
      return reverseChars

print reverseString("Live a happy life.")

 

Return to Top

Main Index

(c) Compiled by B V Wood.

 Main Index

UK SPONSERS

 
USA SPONSERS