Reverse a string in python; In this tutorial, you will learn how to reverse a string in python by using for loop, recursion function, slicing, while loop, stack and without using inbuilt function.
If you are working with string in the Python programming language. And want to reverse the string in Python. There are many ways to reverse the string in python such as reversing a string in python using for loop, reverse a string in python using while loop, reversing a string in python without using reverse function, reverse string in python without using the function.
If you want to reverse the string in Python. So you search in Google in many ways. Such as how to reverse a string in python using for loop, how to reverse a string in python using a while loop, how to reverse a string in python without using the reverse function, how to reverse a string in python without using the function.
How to Reverse String in Python
See the following python program for how to reverse a string in Python using for loop, using while loop, and without built-in function of Python; as shown below:
- How to reverse a string in python using for loop
- Python reverse string using slicing
- Python reverse string using recursion
- How to reverse a string in python using while loop
- Python Program to Reverse String Using Stack
1: How to reverse a string in python using for loop
Let’s create the first program now. Which will reverse the string using for loop in Python.
python program to reverse a string using for loop:
# Python program to reverse a string # using for loop def reverse(s): str = "" for i in s: str = i + str return str str = "Python World" print ("Given string is : ",end="") print (str) print ("Reversed string using loops is : ",end="") print (reverse(str))
Output
Given string is : Python World Reversed string using loops is : dlroW nohtyP
2: Python reverse string using slicing
Now next, now we will reverse the string using slicing in Python.
To reverse a string in python without using the reverse function. python program to reverse a string using slicing:
# Python program to reverse a string # using slice # Function to reverse a string def reverse(string): string = string[::-1] return string str = "Python World" print ("Given string is : ",end="") print (str) print ("Reversed string using extended slice is : ",end="") print (reverse(str))
Output
Given string is : Python World Reversed string using extended slice is : dlroW nohtyP
3: Python reverse string using recursion
Now reverse the string in Python by using the recursive.
python program to reverse a string using recursion:
# Python program to reverse a string # using recursion def reverse(s): if len(s) == 0: return s else: return reverse(s[1:]) + s[0] str = "Python World" print ("Given string is : ",end="") print (str) print ("Reversed string using recursion is : ",end="") print (reverse(str))
Output
Given string is : Python World Reversed string using recursion is : dlroW nohtyP
4: How to reverse a string in python using while loop
If you are looking for how to reverse a string in python using while loop. The Python program below is for you.
reverse a string in python using while loop:
# Python program to reverse a string # using while loop def reverse_string(input): string = "" length = len(input) - 1 while length >= 0: string = string + input[length] length = length - 1 return string str_data = 'My world' print ("Given string is :" + str_data) if __name__ == "__main__": print('Reverse String using While Loop =', reverse_string(str_data))
Output
Given string is :My world Reverse String using While Loop = dlrow yM