Friday, October 31, 2014

Python - Reverse the String

I am trying to demonstrate one or two methods (among many to achieve the stated problem).

First Method:
 FirstString="First String"  
 print (FirstString [::-1])  
Above example is extended slice syntax. It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.

Second Method:
 SecondString="Second String"  
 def reverse(text):  
   if len(text) <= 1:  
     return text  
   return reverse(text[1:]) + text[0]  
 print(reverse(SecondString))  
You can try to run this code on Codebunk. You can refer to my Codebunk. These are simple code which can be used in complex algorithm.

I am glad to add below video in my blog created by Webucator. This video explains the performance of two methods demonstrated above.


For more details on python training visit, Webucator-Python

More suggestion are always welcome. In case if you wish me to write about any specific scenario, please feel free to write to me or else you can Comment below.

No comments:

Post a Comment