DEV Community

Cover image for Two Easiest way to reverse the string in python👍🤪
kishan singh
kishan singh

Posted on

Two Easiest way to reverse the string in python👍🤪

1)USING FOR LOOP❤️

def reverse(string):
rev_string=""
for i in string:

    rev_string= i+rev_string
print("reversed string:",rev_string)
Enter fullscreen mode Exit fullscreen mode

string="kishan"
reverse(string)

output:Image description

2) USING RECURSION🚇

def reverse(s):
if len(s) == 0:
return s
else:
return reverse(s[1:]) + s[0]

s = "hello brain"

print ("The original string is : ",end="")
print (s)

print ("The reversed string(using recursion) is : ",end="")
print (reverse(s)

output:
Image description

Top comments (0)