Given a string containing a line of text the input to your program
the program should print the string in reverse order. Until d or done
is entered as a input text
The program should run infinitely until the user quits it by entering done.
Write
a Python Program That Takes in a Line of Text as Input And Outputs That
Line of Text in Reverse The Program Repeats Ending When The User Enters
Done Done Or d For The Line of Text
Using the infinite while
loop iterates the set of statements infinitely. ask a user to enter a
text, compare the text with “d” or “done” if the text is “d” or “done”
then exit the program. Take an else block and reverse the user entered
string and print the text in reverse order.
while True:
text = input("\nEnter a Text : ")
if text.lower() == "done" or text.lower() == "d":
print("Exiting the Program Bye !")
exit(0)
else:
print("Text in Reverse is ...")
print(text[::-1])
To reverse a string use the string slicing technique that is [::-1], which should be used to reverse the string in Python.
Complete Python Working Code using String Slicing Method
================================================
while True:
text = input("\nEnter a Text : ")
if text.lower() == "done" or text.lower() == "d":
print("Exiting the Program Bye !")
exit(0)
else:
print("Text in Reverse is ...")
print(text[::-1])
You
can also use the for loop to reverse a string as shown below. that is
take each and every letter from the text and append it in the front of
the empty string variable named data. once the follow completes print
the the variable named data
Python program to reverse a string using the for loop
==========================================
while True:
text = input("\nEnter a Text : ")
if text.lower() == "done" or text.lower() == "d":
print("Exiting the Program Bye !")
exit(0)
else:
print("Text in Reverse is ...")
data = ""
for item in text:
data = item + data
print(data)
OUTPUT
====================
Enter a Text : hello
Text in Reverse is ...
olleh
Enter a Text : this is the program
Text in Reverse is ...
margorp eht si siht
Enter a Text : python program
Text in Reverse is ...
margorp nohtyp
Enter a Text : done
Exiting the Program Bye !
OUTPUT 2
======================
Enter a Text : this is run number 2
Text in Reverse is ...
2 rebmun nur si siht
Enter a Text : d
Exiting the Program Bye !
Conclusion
=======================
Execute
the program by yourself and provide the input text randomly to the
Python program and not down the results. use the string d or done to
exit the program
Comment down below if you have any suggestions or queries.
0 Comments