Program 4: “Invert a String” The intent of this program is to allow a user to enter a string of any length (can be a sentence), and then print the string with the characters in reverse order. Use only a “for loop” to invert the input.
Here's a simple Python program that inverts a string using a for loop, following the specifications you've provided:
# Program Header
print("########## Program #4 – Invert a String ###########")
# Prompt for user input
user_input = input("Please enter a string: ")
# Initialize an empty string to hold the inverted result
inverted_string = ""
# Use a for loop to construct the inverted string
for i in range(len(user_input) - 1, -1, -1):
inverted_string += user_input[i]
# Output the result
print(f'"{user_input}" is my string and it’s inverted to "{inverted_string}"')
You can run this code in any Python environment to see how