How to reverse a string with preserving the spaces using python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to reverse a string with preserving the spaces using python

21st Nov 2019, 12:20 AM
R Vijay Anand
R Vijay Anand - avatar
3 Answers
+ 1
Just: your_string = your_string[::-1] (Or, if it's the *position* of the spaces which you want to preserve, what Błack Jesus❕ said.)
21st Nov 2019, 8:58 AM
HonFu
HonFu - avatar
- 1
Can you write a python program and give?
21st Nov 2019, 12:42 AM
R Vijay Anand
R Vijay Anand - avatar
- 1
A simple way to do it: new_string = ''.join(list(old_string)[::-1]) I hope that is good enough for you!
21st Nov 2019, 8:28 AM
Gonçalo Magalhaes
Gonçalo Magalhaes - avatar