Reverse a string
0 votos
October 9th, 2006 toydi Posted in python, rev | Hits: 4848 |
python provides a -c option to accept and run a script line.
To reverse a string like this one: “toydi deified idiot”.
echo 'toydi deified idiot' | python -c "print raw_input()[::-1]”
echoprints the string to stdout.pythonrun the script line.raw_input()read from stdin (to read from pipe and return the string).[::-1]simply says: steps through and return each character in the string, but in reverse order (-1).printprints the reversed string to stdout:
toidi deified idyot
Have fun with python!
Wait.. ok, actually, rev will just do the job:
echo "toidi deified idyot" | rev
Live Chat!









October 9th, 2006 at 5:24 pm
I thought this was Linux by Examples, not Python :P.
So I’m going to add some ruby mix to the fun.
You’ll get an extra line at the top because it also reversed the new line character.
October 12th, 2007 at 6:29 pm
echo -ne “string” does not generate newline
June 10th, 2008 at 1:44 pm
echo “ruby r roxors” | ruby -lne ‘puts $_.reverse’