Online Tutorials & Training Materials | STechies.com
Register Login

5 Different Ways to Reverse String in Python

|| 0

5 Different Ways to Reverse String in Python
Stechies

In python string library, there is no in-build “reverse” function to reverse a string, but there are many different ways to reverse a string. 

In this article you will learn 5 different ways to reverse the string in Python.

Using for Loop

Example:

# Program to explain reverse string or sentence 
# Using for loop
# Reverse String without using reverse function

# Define a function
def reverse_for(string):
    # Declare a string variable 
    rstring = ''

    # Iterate string with for loop
    for x in string:
        # Appending chars in reverse order
        rstring = x + rstring
    return rstring

string = 'Stechies'

# Print Original and Reverse string
print('Original String: ', string)
print('Reverse String: ', reverse_for(string))

Output:

Original String:  Stechies
Reverse String:  seihcetS

Explanation:

In the above example we use for loop to iterates through every character of  the string and append each character in the beginning, in order to get the final string as reverse string.

Using while loop

Example:

# Program to explain reverse string
# Using while loop

# Define a function
def reverse_while(string):
    # Declare a string variable 
    rstring = ''

    # We will calculate string length
    # And subtract 1 because string index start with 0
    length = len(string) - 1
    # We will run the loop with last string index 
    while length >= 0:
        print('String Index: ',string[length],' - ', length)

        #Appending chars in reverse order
        rstring = rstring + string[length]

        # Decrements string index by 1
        length = length - 1
    return rstring

string = 'Stechies'
# Print Original and Reverse string
print('Original String: ', string)
print('Reverse String: ', reverse_while(string))

Output:

Original String:  Stechies
String Index:  s  -  7
String Index:  e  -  6
String Index:  i  -  5
String Index:  h  -  4
String Index:  c  -  3
String Index:  e  -  2
String Index:  t  -  1
String Index:  S  -  0
Reverse String:  seihcetS

Explanation:

In the above example we first subtracting the length of the string by one because the string index starts from zero. 

Iterates through every element of string in reverse order using while loop and append character in final string in reverse order.

Using Slicing

Example:

# Program to explain reverse string
# Using Slicing

# Define a function
def reverse_slice(string):
    return string[::-1]

string = 'Stechies'
# Print Original and Reverse string
print('Original String: ', string)
print('Reverse String: ', reverse_slice(string))

Output:

Original String:  Stechies
Reverse String:  seihcetS

Explanation:

In the example we are using the slicing to slice the string from start to end and moving backwards to the beginning of the string.

Slice provides steps such as [start,stop,step]. As you can see in the above example we did not give any filed to start & stop, so it will take the default value as 0 for start and stop and (-1) to step respectively which denotes starting from end and stop at the start while reversing the string.

Using join() and reversed()

Example:

# Program to explain reverse string
# Using for reversed() function 

# Define a function
def reverse_rev(string):
    # Join reverse object of original string
    rstring = ''.join(reversed(string))
    return rstring

string = 'Stechies'

# Print Original and Reverse string
print('Original String: ', string)
print('Reverse String: ', reverse_rev(string))

Output:

Original String:  Stechies
Reverse String:  seihcetS

Explanation:

In the above example you can see that we pass the string using reversed() method to iterate every element of the string in reverse order and by using join() function, we join the element of the string.

Using List reverse()

Example:

# Program to explain reverse string
# Using for reverse() function 

# Define a function
def reverse_list(string):
    # Convert string to list
    rstring  = list(string)

    # Print List
    print(rstring)

    # Reverse List with reverse() function
    rstring.reverse()

    # Print reverse list
    print(rstring)

    # Convert list to string with join() function 
    return ''.join(rstring)

string = 'Stechies'

# Print Original and Reverse string
print('Original String: ', string)
print('Reverse String: ', reverse_list(string))

Output:

Original String:  Stechies
['S', 't', 'e', 'c', 'h', 'i', 'e', 's']
['s', 'e', 'i', 'h', 'c', 'e', 't', 'S']
Reverse String:  seihcetS

Explanation:

In the above example we are converting string to a list and reversing the list using reverse() function then converting the list to string using join() method.