6.py 744 B

123456789101112131415161718192021222324252627282930
  1. import os
  2. def printReverse(string):
  3. # Get length of string
  4. stringLength = len(string)
  5. # Variable holding the new reversed string
  6. newString = ""
  7. # Position of first letter is considered 0 in string
  8. # Set i to position of last letter in string
  9. i = stringLength - 1
  10. # Iterate the string backwards (count backwards from length of string down to zero)
  11. while i >= 0:
  12. # Add character of position i to new reversed string
  13. newString = newString + string[i]
  14. # Decrease counter by 1
  15. i -= 1
  16. print(newString)
  17. # Clear screen
  18. os.system("clear") # Mac or Linux
  19. #os.system("cls") # Windows
  20. userInput = input("Input a string: ")
  21. # Call printReverse function
  22. printReverse(userInput)