[Tutor] how to print a message backwards

ALAN GAULD alan.gauld at btinternet.com
Thu Sep 24 01:40:42 CEST 2009


> Is there a significant difference in speed, style, or any pythonesque
> reasoning between Alan's solution and 
> print message[::-1]


Yes, the for loop doing one character at a time will be much 
slower than using a slice. The slice is more pythonic but less general.
A reverse loop is something that is often needed in programming 
solutions so it's useful to know how to do it for the general case. 
But where the data  can be sliced that will be faster and more 
concise.

Alan G.


More information about the Tutor mailing list