About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Sunday, September 1, 2019

Reversing a string in Python

It is quite simple in Python, it can be done easily using step parameter of string extract

>>> str = 'reverse'
>>> str[::-1]
'esrever'

With the help of step = -1, we can easily reverse any string in python.

Lets create a function, which will reverse the given string and then check if reverse = actual then return true else false.

def reverseStr(str):
   
if str[::-1] == str:
       
print('The result is True')
   
else:
       
print('The result is False')

str =
'liril'
reverseStr(str)

Output:
True

So here you can assign str with value ‘liril’ which in turn is passed to the function and the result was equal to the reverse of the string passed that’s why the result was true.


Data Science with…Python J
Post Reference: Vikram Aristocratic Elfin Share

No comments:

Post a Comment