string = input("Enter a string:")
reverse = ""
for i in string:
reverse = i + reverse
print("Reverse:", reverse)
Learn coding with simple code, clear explanations, and examples
string = input("Enter a string:")
reverse = ""
for i in string:
reverse = i + reverse
print("Reverse:", reverse)
Enter a string:Welcome
Reverse: emocleW
Enter a string:Hello world
Reverse: dlrow olleH
Examples of reversing a given string:
Example #1
Welcome
reverse of the string is emocleW.
Example #2
Hello world
reverse of the string is dlrow olleH.
Here i will explain the code with an example.
string = input("Enter a string:")
The input() function is used to get input from the user during runtime.
For example: if the user input is "Welcome", it will be assigned to the variable 'string' as str(string) data type object.
string = "Welcome"
reverse = ""
The variable 'reverse' is initialised to empty string.
for i in string:
reverse = i + reverse
#First loop
for i in string:
=> for i in "Welcome": Here, String character "W" is assigned to the variable 'i' in this first loop which is True, therefore, the body of the loop will be executed:
reverse = i + reverse
=> reverse = "W" + ""
=> reverse = "W"
By doing the above operation, the variable 'reverse' is stored with the value of 'i + reverse'. Currently the value of the variable 'reverse' is ""(empty string). So, "W" + "" is assigned to the variable 'reverse', ie. "W".
By the end of the first loop, the current value of variable 'reverse' is as shown below.
reverse = "W"
#Second loop
for i in string:
=> for i in "Welcome": Here, String character "e" is assigned to the variable 'i' in this second loop which is True, therefore, the body of the loop will be executed:
reverse = i + reverse
=> reverse = "e" + "W"
=> reverse = "eW"
By doing the above operation, the variable 'reverse' is stored with the value of 'i + reverse'. Currently the value of the variable 'reverse' is "W". So, "e" + "W" is assigned to the variable 'reverse', ie. "eW".
By the end of the second loop, the current values of variables are as shown below.
reverse = "eW"
#Third loop
for i in string:
=> for i in "Welcome": Here, String character "l" is assigned to the variable 'i' in this third loop which is True, therefore, the body of the loop will be executed:
reverse = i + reverse
=> reverse = "l" + "eW"
=> reverse = "leW"
By doing the above operation, the variable 'reverse' is stored with the value of 'i + reverse'. Currently the value of the variable 'reverse' is "eW". So, "l" + "eW" is assigned to the variable 'reverse', ie. "leW".
By the end of the third loop, the current values of variables are as shown below.
reverse = "lew"
#Fourth loop
for i in string:
=> for i in "Welcome": Here, String character "c" is assigned to the variable 'i' in this fourth loop which is True, therefore, the body of the loop will be executed:
reverse = i + reverse
=> reverse = "c" + "leW"
=> reverse = "cleW"
By doing the above operation, the variable 'reverse' is stored with the value of 'i + reverse'. Currently the value of the variable 'reverse' is "leW". So, "c" + "leW" is assigned to the variable 'reverse', ie. "cleW".
By the end of the fourth loop, the current values of variables are as shown below.
reverse = "clew"
#Fifth loop
for i in string:
=> for i in "Welcome": Here, String character "o" is assigned to the variable 'i' in this fifth loop which is True, therefore, the body of the loop will be executed:
reverse = i + reverse
=> reverse = "o" + "cleW"
=> reverse = "ocleW"
By doing the above operation, the variable 'reverse' is stored with the value of 'i + reverse'. Currently the value of the variable 'reverse' is "cleW". So, "o" + "cleW" is assigned to the variable 'reverse', ie. "ocleW".
By the end of the fifth loop, the current values of variables are as shown below.
reverse = "oclew"
#Sixth loop
for i in string:
=> for i in "Welcome": Here, String character "m" is assigned to the variable 'i' in this sixth loop which is True, therefore, the body of the loop will be executed:
reverse = i + reverse
=> reverse = "m" + "ocleW"
=> reverse = "mocleW"
By doing the above operation, the variable 'reverse' is stored with the value of 'i + reverse'. Currently the value of the variable 'reverse' is "ocleW". So, "m" + "ocleW" is assigned to the variable 'reverse', ie. "mocleW".
By the end of the sixth loop, the current values of variables are as shown below.
reverse = "moclew"
#Seventh loop
for i in string:
=> for i in "Welcome": Here, String character "e" is assigned to the variable 'i' in this seventh loop which is True, therefore, the body of the loop will be executed:
reverse = i + reverse
=> reverse = "e" + "mocleW"
=> reverse = "emocleW"
By doing the above operation, the variable 'reverse' is stored with the value of 'i + reverse'. Currently the value of the variable 'reverse' is "mocleW". So, "e" + "mocleW" is assigned to the variable 'reverse', ie. "emocleW".
By the end of the seventh loop, the current values of variables are as shown below.
reverse = "emoclew"
#Eight loop [not executed]
for i in string:
=> for i in "Welcome": all the string characters are accessed one by one and the end of the string is reached,therefore, the body of the loop will not be executed and control of the loop comes outside the loop.
print("Reverse:", reverse)
Now the statement 'print("Reverse:", reverse)' will get executed and the output will be 'Reverse: emocleW'.