5 Ways to Reverse String in Python

综合编程 2017-12-31

Hello everyone, in this tutorial we’ll see different ways to reverse string in Python.

As we know, we can reverse a list
using reverse()
method but Python doesn’t have the reverse()
method for string.

Here are some alternate and easy ways to reverse a string.

Ways to Reverse String in Python

1. Using Loop

string1 =  "the crazy programmer"
string2 = ""
i = len(string1)-1
  string2 = string2 + string1[i]
  i = i-1
print "original = " + string1
print "reverse  = " + string2


original = the crazy programmer

reverse = remmargorp yzarc eht

In above program, we’ve started a loop from the last index (length-1) to first index (0) of string1. In each step of loop, it will pick the character from right-side in string1 and concatenate with string2.

2. Using Recursion

def reverse_it(string):
  if len(string)==0:
    return string
    return reverse_it(string[1:]) + string[0]
    print "added " + string[0]
string1 = "the crazy programmer"
string2 = reverse_it(string1)
print "original = " + string1
print "reversed = " + string2

In above program, there is a reverse_it()
method which accepts a string and then it will check if the string is empty or not, if empty then it will return the string otherwise it will call itself by passing string from its second character to last character.

String = “hello”

Print string[1:]

Output: ‘ello’

After calling reverse_it()
method again and again there will be a point when string will be empty then the condition

if len(string) == 0:

will be true , it will return the string. return
statement will throw the execution, where it came from.

So in

Return reverse_it(string[1:]) + string[0]

the “+ string[0] “
will be executed next, which will add the first letter at last.

3. Using Stack

def create_stack():
  #it will  create a List named as stack and return it
  stack = []
  return stack
def push(stack,element):
  #it will add a new element to List
def pop(stack):
  #it will delete the last element from  List
  if len(stack) == 0:
  return stack.pop()
def reverse(string):
  #method to reverse the string using stack's functions
  n = len(string)
  #to create a empty list (stack)
  stack = create_stack()
  #inserting character of string into List
  for i in range(0,n):
  #making string empty
  string = ""
  #getting last element of the List (stack) and storing it into string
  for i in range(0,n):
    string = string + pop(stack)
  return string
string1 = "the crazy programer"
string2 = reverse(string1)
print "original = " + string1
print "reversed = " + string2

In above program, we’re using concept of stack having push and pop

To implement stack concept we’re using list.

When we call reverse()
method, it will create a list
named as ‘stack’ and insert all the characters of string into list using push()
method. At last it will fetch all the elements in the list
from last to first one by one and store them into the string.

4. Using Extended Slice

string = "the crazy programmer"
print "original = " + string
string = string[::-1]
print "reversed = " + string

Mostly extended slice is used for skipping the steps but if we put -1 in third ‘step’
or ‘stride’
argument then we can get the reverse of a string, list and tupple.

5. Using List

string = "the crazy programmer"
print "original = " + string
#convrting string into list
list1 = list(string)
#applying reverse method of list
#converting list into string
string = ''.join(list1)
print "reversed = " + string

String doesn’t have reverse()
method but lists
have. So we are converting string into list, performing reverse()
operation and again converting it back into string using ‘ ’.join()

Comment below if you have queries or know any other way to reverse a string in python.

The Crazy Programmer

责编内容by:The Crazy Programmer (源链)。感谢您的支持!


Meet the Julians JuliaCon , the annual gathering of the community focused on the Julia programming language, convened last week at MIT. I hung out there for a couple ...
Europilot: Create self-driving trucks inside Euro ... europilot Overview Europilot is an open source project that leverages the popular Euro Truck Simulator(ETS2) to develop self-driving algorithms. ...
资深程序员用Python爬取了11万Java程序员!Java原来是这样的呢!... 我们爬了 11 万个 Java 人的信息进行画像分析 Java 主要集中地区: 北上广深以 80 迈的速度领跑。成都、杭州、武汉、南京、西安发展势头迅猛。剩下的小可爱们,接下来的日子谁会后来者居上呢?让我们拭目以待。 工作经验主要集中区域: 在...
What is behavior-driven Python? Have you heard about behavior-driven development (BDD) and wondered what all the buzz is about? Maybe you've caught team members talking in "gherk...
Python Gone Bad Not that long ago perl was the most common go-to language for developers, and many claimed its syntax was nothing more than line noise. Python should ...