Python | Reverse a string without recursion

Let us now see all the methods to solve Python programs to reverse a string without recursion.

Method 1: Using the loop

def rev_my_string(s):
	str = ""
	for i in s:
		str = i + str
	return str

s = input(">> Enter the string you want to reverse: ")
print("\n")

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

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

Output

>> Enter the string you want to reverse: tnemngissAypoC - nohtyP fo s'GO eht era ew

>> The original string is : tnemngissAypoC - nohtyP fo s'GO eht era ew

>> The reversed string(without using recursion) is : we are the OG's of Python - CopyAssignment

Explanation

We used the for loop and inside the for loop, we assigned the value that is stored in ” i “ to the str variable. In the first iteration, the str will be empty but as the values accessed by the ” i “will be stored, it will start forming the reversed string.

Method 2: Using the concept of stack

def make_a_stack():
	stack = []
	return stack
def check_size(stack):
	return len(stack)
def check_stack_empty(stack):
	if check_size(stack) == 0:
		return true
def push_to_stack(stack, item):
	stack.append(item)
def pop(stack):
	if check_stack_empty(stack):
		return
	return stack.pop()
def rev_my_string(string):
	n = len(string)
	stack = make_a_stack()
	for i in range(0, n, 1):
		push_to_stack(stack, string[i])
	string = ""
	for i in range(0, n, 1):
		string += pop(stack)
	return string
s = input(">> Enter the string you want to reverse: ")
print("\n")
print(">> The original string is : ", end="")
print(s)
print("\n")
print(">> The reversed string(using recursion) is : ", end="")
print(rev_my_string(s))

Explanation

In this method, we declared 6 types of functions. Each function will handle some or the other thing.
The main logic behind using stack in this is not the stack data structure. As in stack data structure, where can perform push and pop, those concepts we will be using here to solve the given problem.

At first, the string is passed on to the rev_my_string function. Here the length is determined and a stack is defined. make_a_stack is used to make a stack, check_size is used to check the size of the stack, check_stack_empty is used to check whether the stack is empty or not, push_to_stack is used to push the elements and finally, pop() is used to delete any element. These continuous calling of function is basically done to perform the operations in the stack.

Method 3: Using string slicing concept

def rev_my_string(string):
	string = string[::-1]
	return string

s = input(">> Enter the string you want to reverse: ")
print("\n")

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

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

Explanation:

Here we used the string-slicing concept. We direct used the negative indexing concept.

Method 4: Using the reverse() function

def rev_my_string(string):
    string = list(string)
    string.reverse()
    return "".join(string)
 
s = input(">> Enter the string you want to reverse: ")
print("\n")
print(">> The original string is : ", end="")
print(s)
print("\n")
print(">> The reversed string(using recursion) is : ", end="")
print(rev_my_string(s))

Explanation

Here we used the built-in function named reverse(). We passed on our string to the given built-in function and then joined it using join().

Method 5: Using the concept of list comprehension

def rev_my_string(string):
	string = [string[i] for i in range(len(string)-1, -1, -1)]
	return "".join(string)

s = input(">> Enter the string you want to reverse: ")
print("\n")

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

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

Explanation:

In this method, we used a one-liner to solve this problem/ The first part in the list comprehension is expression then for loop, and finally our conditional statement. When you wish to make a new list based on the values of an existing list, list comprehension offers a more concise syntax.

Here is the end of our article. We hope it had new ways and concepts to boost your Python knowledge.

Share:

Author: Dhvanil V