I want to reverse a text which is in variable in python but I'm not sure if we have string method for this problem or not How can i reverse the text with strings method?
Asked
Active
Viewed 142 times
-3
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Aug 02 '22 at 21:42
-
"*I'm not sure if we have string method for this problem or not*" You don't need to guess, you can check the Python docs for all the string methods: https://docs.python.org/3/library/stdtypes.html#string-methods (spoilers: there is no reverse method). – Gino Mempin Aug 02 '22 at 21:50
-
One way to *"kill two birds with a nuclear-powered sledgehammer"* would be to use `reversed` iterator to join back to a str: `''.join(reversed('hello world!'))` – rv.kvetch Aug 02 '22 at 21:55
-
On that page, reverse() is indeed listed as a method. Indeed, "reverse" is mentioned exactly 20 times at the documentation URL given. – Hugh Aug 02 '22 at 22:12
2 Answers
1
A simple google search would let you find how to work with lists and variables. You can use: [::-1] at the end of the list or variable to reverse it. Then you can customize the iterators if you need to increment at different intervals.
var = "123"
print(var[::-1]) # "321"

George Reyes
- 31
- 3