• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Reverse String with Recursion in Python

Reverse String with Recursion in Python

February 15, 2022 Leave a Comment

In Python, we can reverse a string with recursion using a recursive function.

string = "Hello"

def reverse_string(string):
    if len(string) == 1:
        return string
    return reverse_string(string[1:]) + string[0:1]

print(reverse_string(string))

#Output:
olleH

When using string variables in Python, we can easily perform string manipulation to change the values or order of the characters in our string.

One such manipulation is to reverse a string. In Python, there is a built-in function called reverse(), but there are other ways we can reverse a string without the reverse() function.

We can reverse a string in Python without the reverse() function with a recursive function.

For recursion, we need to define a base case and a recursive step.

The base case for our recursive reverse function is when our string has a length of one. The recursive step keeps slicing the string from the second character to the end and add the first character to the end.

Below are some examples of how to recursively reverse a string in Python.

string = "Hello"

def reverse_string(string):
    if len(string) == 1:
        return string
    return reverse_string(string[1:]) + string[0:1]

print(reverse_string("Hello"))
print(reverse_string("Good Bye"))
print(reverse_string("My name is Billy"))

#Output:
olleH
eyB dooG
ylliB si eman yM

Reverse a List Without reverse() Function in Python Using Recursion

In an almost identical way, we can reverse a list in Python without the reverse() function using recursion.

To reverse a list using recursion, the base case for our recursive reverse function is when our list has a length of one. The recursive step keeps slicing the list from the second element to the end and add the first element to the end.

Below is an example of how to use recursion to reverse a list in Python.

lst = [1,2,3,4]

def reverse_list(l):
    if len(l) == 1:
        return l
    return reverse_list(l[1:]) + l[0:1]

print(reverse_list(lst))

#Output:
[4,3,2,1]

Hopefully this article has been useful for you to learn how to reverse a string using recursion in Python.

Other Articles You'll Also Like:

  • 1.  Python Get First Word in String
  • 2.  How to Group By Columns and Find Maximum in pandas DataFrame
  • 3.  Python turtle dot() – Draw Dot on Turtle Screen
  • 4.  Get Last Character in String in Python
  • 5.  How to Save and Use Styles from Existing Word File With python-docx
  • 6.  How to Divide Two Numbers in Python
  • 7.  Python Check if List Index Exists Using Python len() Function
  • 8.  Python Remove First Element from List
  • 9.  How to Return Nothing in Python from Function
  • 10.  Using Python to Remove Commas from String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy