Python String is the series of the Unicode character. Python has severa features for string manipulation, however Python string library would not aid the in-built “reverse()” function. But there are a variety of ways to reverse the string. We are defining the following technique to reverse the Python String.
Using for loop
Using while loop
Using the slice operator
Using the reversed() function
Using the recursion
Using for loop
Here, we will reverse the given string the usage of for loop.
def reverse_string(str):
str1 = "" # Declaring empty string to store the reversed string
for i in str:
str1 = i + str1
return str1 # It will return the reverse string to the caller function
str = "JavaTpoint" # Given String
print("The original string is: ",str)
print("The reverse string is",reverse_string(str)) # Function call
Output:
('The original string is: ', 'JavaTpoint')
('The reverse string is', 'tniopTavaJ')
Explanation-
In the above code, we have declared the reverse_string() characteristic and exceeded the str argument. In the feature body, we have declared empty string variable str1 that will hold the reversed string.
Next, the for loop iterated each issue of the given string, be part of each persona in the commencing and store in the str1 variable.
After the entire iteration, it lower back the reverse order string str1 to the caller function. It printed the result to the screen.
Using while loop
We can additionally reverse a string using a while loop. Let’s recognize the following example.
Example –
# Reverse string
# Using a while loop
str = "JavaTpoint" # string variable
print ("The original string is : ",str)
reverse_String = "" # Empty String
count = len(str) # Find length of a string and save in count variable
while count > 0:
reverse_String += str[ count - 1 ] # save the value of str[count-1] in reverseString
count = count - 1 # decrement index
print ("The reversed string using a while loop is : ",reverse_String)# reversed string
Output:
('The original string is : ', 'JavaTpoint')
('The reversed string using a while loop is : ', 'tniopTavaJ')
Explanation:
In the above code, we have declared a str variable that holds string value. We initialized a whilst loop with a price of the string.
In each iteration, the value of str[count – 1] concatenated to the reverse_String and decremented the be counted value. A whilst achieved its generation and again the reverse order string.
Using the slice ([]) operator
We can additionally reverse the given string the use of the prolonged slice operator. Let’s recognize the following example.
Example –
# Reverse a string
# using slice syntax
# reverse(str) Function to reverse a string
def reverse(str):
str = str[::-1]
return str
s = "JavaTpoint"
print ("The original string is : ",s)
print ("The reversed string using extended slice operator is : ",reverse(s))
Output:
('The original string is : ', 'JavaTpoint')
('The reversed string(using extended slice syntax) is : ', 'tniopTavaJ')
Explanation:
Generally, a slice operator accepts three parameters – start, end and step. We provided the no cost to start and quit index, which indicates the begin index is 0 and the cease is n-1 by way of default. The step size is -1; it ability the string continues the traverse from the end and goes to the 1 index position.
Using reverse function with join
Python affords the reversed() feature to reverse the string. Let’s recognize the following example.
Example –
#reverse a string using reversed()
# Function to reverse a string
def reverse(str):
string = "".join(reversed(str)) # reversed() function inside the join() function
return string
s = "JavaTpoint"
print ("The original string is : ",s)
print ("The reversed string using reversed() is : ",reverse(s) )
Output:
('The original string is : ', 'JavaTpoint')
('The reversed string using reversed() is : ', 'tniopTavaJ')
Explanation:
In the feature body, we declared the empty string separated by means of .dot operator. The reversed() string back the reverse string it joined with the empty string separated the usage of the join() function.
Using recursion()
The string can additionally be reversed using the recursion. Recursion is a technique the place feature calls itself. Consider the following example.
Example –
# reverse a string
# using recursion
def reverse(str):
if len(str) == 0: # Checking the lenght of string
return str
else:
return reverse(str[1:]) + str[0]
str = "Devansh Sharma"
print ("The original string is : ", str)
print ("The reversed string(using recursion) is : ", reverse(str))
Output:
('The original string is : ', 'JavaTpoint')
('The reversed string(using reversed) is : ', 'tniopTavaJ')
Explanation:
In the above code, we have defined a function that accepts the string as an argument.
In the function body, we described the base circumstance of recursion, if a length of a string is 0, then the string is returned, and if not then we referred to as the feature recursively.
The slice operator slice the string expects the first personality and concatenated to the give up of the slice string.
Leave a Review