Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • What is GITHUB, and should I use it? github
  • How to show percentage differences between files in Python CSV
  • String Manipulation in Python python method
  • How to Automate Testing With Python | unittest automation
  • how do I declare a null value in python? exception handling
  • How do I fix TypeError: unhashable type: ‘list’ Error? Articles
  • how to compare two lists in Python Python Lists
  • How To Run Python Validation From Javascript Javascript

how to reverse a string in python

Posted on January 24, 2021February 11, 2021 By admin No Comments on how to reverse a string in python

Estimated reading time: 2 minutes

Are you building out your programme and looking to manipulate strings, specifically reverse them?

Here we are going to go through a number of options, bear in mind there maybe more.

Slicing

The first option is slicing. Essentially with slicing you tell the code where to start splitting out the characters, into their constituent parts.

In the below block:

-1 = start the end and work backwards

:: = work from the first element to the last element, until no more elements can be chosen.

Putting this together, take joe, and in reverse order print all the characters until you reach the first value.

name = "joe"[::-1]
print("By slicing: ", name)

Looping through the values of the string

In the below code we have while loop, to achieve the desired effect. The bits to explain in the code are:

strlength = this sets the boundaries of the while loop.

+= This adds whatever variable “strlength” is in the loop to emptylist and saves emptylist for the next iteration until the loop is complete.

str = "joe"
emptylist =[]
strlength = len(str)
while strlength > 0:
    emptylist += str[ strlength -1 ]
    strlength = strlength - 1
print("By using a while loop: ", emptylist)

Using the reverse function

The below takes the string , converts it into a list and then puts the characters in reverse.

As the characters are in a list, in order to get them back together as a string, we use the “.join” function.

org_string = "joe"
createdtemplist = list(org_string)
createdtemplist.reverse()
string_in_reverse = "".join(createdtemplist)
print("By using the reverse function: ", string_in_reverse)

Use list reverse

With list reverse, it is the same as the above approach, except we don’t use .join to get the output back into a string.

This essentially means the final result remains as a list.

string_to_reverse = "joe"
string_list = list(string_to_reverse)
string_list.reverse()
print("By using list reverse: ", string_list)

The output of all this can be seen below

By slicing:  eoj
By using a while loop:  ['e', 'o', 'j']
By using the reverse function:  eoj
By using list reverse:  ['e', 'o', 'j']
strings Tags:join method python, loop through string, reverse a string, reverse function, slicing, slicing strings

Post navigation

Previous Post: select rows with a certain value using SQL
Next Post: TypeError: type object is not subscriptable

Related Posts

  • ValueError: Columns must be same length as key exception handling
  • TypeError: type object is not subscriptable strings
  • How to use wildcards in SQL SQL
  • how to remove spaces from a string regular expressions
  • String Manipulation in Python python method

Leave a Reply Cancel reply

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

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Python tutorial: How to create a graphical user interface in Tkinter Python
  • Tkinter GUI tutorial python – how to clean excel data Python
  • How To Pass Data Between Functions Python Functions
  • How to count the no of rows and columns in a CSV file CSV
  • TypeError: ‘list’ object is not an iterator Python
  • ValueError: pattern contains no capture groups Value Error
  • How to group your data in Tableau data visualisation
  • How to remove characters from an imported CSV file Python Tutorial

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT