Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python reverse tokens in a string

Tags:

python

I have the following string:

apple.orange.red.green.yellow

How can i reverse it to get the following:

yellow.green.red.orange.apple
like image 838
hssss Avatar asked Dec 13 '22 17:12

hssss


2 Answers

I like this (more readable?) one:

>> s = "yellow.green.red.orange.apple"
>> '.'.join(reversed(s.split('.')))
'apple.orange.red.green.yellow'
like image 146
joaquin Avatar answered Jan 01 '23 08:01

joaquin


'.'.join(s.split('.')[::-1])
like image 43
Ignacio Vazquez-Abrams Avatar answered Jan 01 '23 09:01

Ignacio Vazquez-Abrams