write a Python program to read a string and display it in reverse order
Answers
Answer:
Strings can be reversed using slicing. To reverse a string, we simply create a slice that starts with the length of the string, and ends at index 0. The slice statement means start at string length, end at position 0, move with the step -1 (or one step backward).
Explanation:
Hope this helps u ❣
Gummonie ☺
Pls Flw me ☹
Give me thnks ☹
✌ITZ AbUsiNg QuEeN ✌
Required Answer:-
Question:
- Write a Python program to read a string and display it in reverse order.
Solution:
There are different approaches of solving this problem.
1. Using loops.
s=input("Enter a string: ")
for i in range(len(s)-1,-1,-1):
print(s[i],end="")
2. Using string slicing.
s=input("Enter a string: ")
print(s[::-1])
3. In one line.
print(input("Enter a string: ")[::-1])
If you know slicing, you can use second or third approach and if you don't know slicing, follow the first approach.
Explanation of First Approach: Ask the user to enter the string. Iterate 'i' in the range (length_of_string - 1) to 0. Display all the characters in ith index.
See the attachment for output ☑.