+ 3

reverse string in python

how can I reverse a string in python for example if i have a variable called txt = "Ali" how can I reverse it ??

7th Jan 2023, 7:30 PM
Ali M
Ali M - avatar
7 Answers
+ 6
Ali M , the solution print(txt[::-1]) is not indexing it is called *slicing*. here is a link to a short tutorial about slicing in python: https://www.sololearn.com/learn/Python/2453/?ref=app
7th Jan 2023, 8:01 PM
Lothar
Lothar - avatar
+ 3
"Reverse string in Python (6 different ways) - GeeksforGeeks" https://www.geeksforgeeks.org/reverse-string-python-5-different-ways/amp/ Look it there is 6 ways to reverse string in python
9th Jan 2023, 2:37 PM
😇🌟SWATI🌟😇
😇🌟SWATI🌟😇 - avatar
+ 1
oh i searched on google and i found a very good way! using like indexing like in lists !! print(txt[::-1])) well that is good to know, i will leave it, it might help some people !
7th Jan 2023, 7:50 PM
Ali M
Ali M - avatar
+ 1
Ali M there are many ways that can do it but simplest way is str[::-1] and it works also on lists https://code.sololearn.com/c4RSAEkmob8g/?ref=app
8th Jan 2023, 2:39 PM
Sousou
Sousou - avatar
+ 1
txt = "Ali" txt = txt[::-1] print(txt)
9th Jan 2023, 4:12 PM
Patrick ONeil
Patrick ONeil - avatar
0
Lothar thanks for the correction, what about list indexing? how it looks like it has been a long while I didn't practice programing
7th Jan 2023, 8:10 PM
Ali M
Ali M - avatar
9th Jan 2023, 7:31 PM
Rajat Goel
Rajat Goel - avatar