Reverse A String In Python

In the journey of our professional life many times we need to reverse a string so in this blog I'm going to discuss some most common ways that are being used in python to reverse a String. Here I have used Python IDLE 3.11, you can use one like  Anaconda, Pycharm VScode, etc.

Reverse string using Loop

Here I have uses a for loop and reversed the string by reversing the order of characters in the original string

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

s = "Hii Learner"

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

print("The reversed string(using loops) is : ", end="")
print(reverseStr(s))

Output

Reverse String in Python

Figure-Reverse String using Loop

Reverse string in Python by using an extended slice

Here I have used extended slice syntax to reverse a string, and this one is the easiest way to reverse 

# Function to reverse a string
def reverseStr(string):
	string = string[::-1]
	return string

s = "Hii Learner"

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

print("The reversed string(using extended slice syntax) is : ", end="")
print(reverseStr(s))

Output

Reverse string using slice method

Figure-Reverse string using the slice method

Reverse string in Python by using reversed() method

# Python code to reverse a string
# using reversed()

# Function to reverse a string
def reverseStr(string):
	string = "".join(reversed(string))
	return string

s = "Hii Learner"

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

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

Output

Reverse string using the reversed method

Figure-Reverse string using the reversed method

Conclusion 

These are the methods that are being used mostly to reverse a string in python. There are some other methods that have more complexity, but you can use any one based on your requirements.