PDF download Download Article

Learn the best ways to reverse a string when coding in Python

PDF download Download Article

Trying to reverse a string in Python? There are several easy ways to do so! You can use the slice function to reverse the string in 1 line of code. Alternatively, use a For loop or the reversed() function for additional flexibility in the reversal process. You can also use the join() function or a list to make a string backwards. This wikiHow guide shows 6 tricks to reverse a string in Python.

Things You Should Know

  • Using a slice is the quickest, simplest way to reverse a string.
  • Use a For loop if you need more customization in the reversal process.
  • Try the reversed() function to create a reversed iterable object from the string.
Method 1
Method 1 of 6:

Using a Slice

PDF download Download Article
  1. Python, the easy-to-learn programming language, has several methods for working with strings. This method uses the slice function to reverse the string. Open your Python file or start a new one. Then use the assignment operator = to assign the string you want to reverse to a variable. For example:
    • a = "example string"
  2. This is a slice statement that begins at the end of the string, ends at the beginning of the string (position 0), and steps by 1 backwards through the string (indicated by the negative 1). Following our example:[1]
    • a = "example string"[::-1]
    Advertisement
  3. The string assigned to a has been reversed by the slice. To see the reversed string, use the print() function:
    • print(a)
    • For our example, “gnirts elpmaxe” would print in the terminal.
  4. Advertisement
Method 2
Method 2 of 6:

Creating a Slice Function

PDF download Download Article
  1. This method creates a function that uses a slice to reverse the string. Use the assignment operator = to assign the string you want to reverse to a variable. For example:
    • f = "example string"
  2. This function will return a reversed string.
    • def backwards(text):
          return text[::-1]
  3. You can now apply this function to any string that you want to reverse. Following our example:
    • f_reverse = backwards(f)
  4. The string assigned to f has been reversed by the new function. To see the reversed string f_reverse, use the print() function:
    • print(f_reverse)
    • For our example, “gnirts elpmaxe” would print in the terminal.
  5. Advertisement
Method 3
Method 3 of 6:

Using a For Loop

PDF download Download Article
  1. This method creates a loop to reverse the string. Use the assignment operator = to assign the string you want to reverse to a variable. For example:
    • b = "example string"
    • You can convert an integer to a string if needed.
  2. This method will append the characters in the original string backwards to a new blank variable. To create a blank variable, assign a new variable to "". For example:
    • b_reverse = ""
  3. This For loop will iterate through the original string, appending each letter to the beginning of the new variable. This will reverse the string. Continuing our example:
    • for i in b:
          b_reverse = i + b_reverse
  4. The string assigned to b has been reversed by the For loop. To see the reversed string b_reverse, use the print() function:
    • print(b_reverse)
    • For our example, “gnirts elpmaxe” would print in the terminal.
  5. Advertisement
Method 4
Method 4 of 6:

Using reversed()

PDF download Download Article
  1. There are many functions to learn, especially if you’ve just started programming in Python. This method uses the reversed() function to reverse the string. Use the assignment operator = to assign the string you want to reverse to a variable. For example:
    • c = "example string"
  2. This function reverses the order of an iterable object, like a string. To use the reverse function, create a new variable assigned to the reversed original string:[2]
    • c_backward = reversed(c)
  3. This new variable will be used to create the reversed string. The reversed function doesn’t return a string, so a new string variable is needed.
    • c_new = ""
  4. This loop will append the reversed list of characters to the new string variable c_new.
    • for i in c_backward:
          c_new = c_new + i
  5. The string assigned to c has been reversed by the reversed() function and For loop. To see the reversed string c_new, use the print() function:
    • print(c_new)
    • For our example, “gnirts elpmaxe” would print in the terminal.
  6. Advertisement
Method 5
Method 5 of 6:

Using join()

PDF download Download Article
  1. This method uses the join() and reverse() to reverse the string. Use the assignment operator = to assign the string you want to reverse to a variable. For example:
    • e = "example string"
  2. The reverse() function reverses all of the items in the string and turns it into an iterator type. Then, the join() function combines the items into one string. Following our example, the code is written:
    • e_reverse = "".join(reversed(e))
  3. The string assigned to {{kbd|e} has been reversed by the reversed() and join() function. To see the reversed string e_reverse, use the print() function:
    • print(e_reverse)
    • For our example, “gnirts elpmaxe” would print in the terminal.
  4. Advertisement
Method 6
Method 6 of 6:

Using a List

PDF download Download Article
  1. This method uses a list to reverse the string. Use the assignment operator = to assign the string you want to reverse to a variable. For example:
    • d = "example string"
  2. This list will contain the individual characters in the string.
    • letter_list = []
  3. This string will be where the characters are placed in reverse order.
    • d_reverse = ""
  4. You can use the append() function to do so:
    • for j in d:
          letter_list.append(j)
  5. Use the plus (+) operator to do so. Place the iterator first and the reverse string variable second to add each character to the front of the string.
    • for k in letter_list:
          d_reverse = k + d_reverse
  6. The string assigned to d has been reversed by the list and For loops. To see the reversed string d_reverse, use the print() function:
    • print(d_reverse)
    • For our example, “gnirts elpmaxe” would print in the terminal.
  7. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Video

Tips

  • These aren’t the only ways to reverse a string in Python! Experiment with different functions and loops to get the exact results you’re looking for.
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Use Windows Command Prompt to Run a Python FileUse Windows Command Prompt to Run a Python File
Update Pip Upgrade Pip: Quick Guide for Beginners & Experts
Open a Python FileOpen a Python File
Pip Uninstall Package Uninstall a Python Package With Pip: Step-by-Step Guide
Comment Out Multiple Lines in Python Comment Out Multiple Lines in Python: Formatting & Shortcuts
Install Pip on Mac3 Easy Ways to Download & Install Pip on Your Mac
Check Python Version on PC or Mac See the Python Version on Mac, Windows, and Linux
Start Programming in PythonStart Programming in Python
Change the Font Size in Python ShellChange the Font Size in Python Shell
Uninstall PythonUninstall Python
Program a Game in Python with PygameProgram a Game in Python with Pygame
Make a Countdown Program in Python Code a Python Countdown Timer: Step-by-Step Guide
Write a Coin Flipping Program on PythonWrite a Coin Flipping Program on Python
Create Loops in Python Create Loops in Python
Advertisement

About This Article

Kyle Smith
Written by:
wikiHow Technology Writer
This article was co-authored by wikiHow staff writer, Kyle Smith. Kyle Smith is a wikiHow Technology Writer, learning and sharing information about the latest technology. He has presented his research at multiple engineering conferences and is the writer and editor of hundreds of online electronics repair guides. Kyle received a BS in Industrial Engineering from Cal Poly, San Luis Obispo. This article has been viewed 7,176 times.
How helpful is this?
Co-authors: 4
Updated: March 20, 2023
Views: 7,176
Categories: Python
Thanks to all authors for creating a page that has been read 7,176 times.

Is this article up to date?

Advertisement