How to Reverse a String in Python

String:

Arrays are Strings. Strings in Python, like many other common programming languages, are sequences of bytes that represent unicode characters. However, since Python lacks a character data form, a single character is simply a one-length string. Square brackets may be used to access the string’s components.

Given a string , the task is to reverse the given string in Python.

Examples:

Example1:

Input:

given_string="vikram"

Output:

The reversed string of vikram = markiv

Example2:

Input:

given_string="wor45rd"

Output:

The reversed string of wor45rd = dr54row

Explanation:

Here 4,5 are also considered as character so it is reversed succesfully

Example3:

Input:

given_string="BTechGeeks"

Output:

The reversed string of BTechGeeks = skeeGhceTB

Here it ignores the case so the reversed string contains many uppercase characters.

Reverse the string in Python

There are several ways to reverse the string in python some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1: Using Slicing

Approach:

  • Scan the given string.
  • Reverse the string using slicing using [::-1] which indicates that we traverse from end
  • Print the reversed string.

Below is the implementation:

# given string
given_string = "vikram"
# calculating the length of string
length = len(given_string)
# Reversing the string using slicing
reverse_string = given_string[len(given_string)::-1]
# print the reversed string
print("The reversed string of", given_string, "=", reverse_string)

Output:

The reversed string of vikram = markiv

Method #2: Using join() and reversed function

We can reverse the string easily by using join() and reversed string like below.

Approach:

  • Scan the given string.
  • join the reversed string using join() function.(We can reverse the string while joining using reversed function).
  • Store it in any variable say rev_string
  • Print the reversed rev_string.

Below is the implementation:

# given string
given_string = "vikram"
# reversing using join and reversed function
rev_string = ''.join(reversed(given_string))
# print the reversed string
print("The reversed string of", given_string, "=", rev_string)

Output:

The reversed string of vikram = markiv

Method #3:Using for loop and string concatenation

Approach: 

  • Scan the givenstring.
  • Take a empty string say revstring.
  • Traverse every character of the string using for loop in reverse order using range function.
  • Add each character to revstring using string concatenation.
  • print the revstring.

Below is the implementation:

# given string
given_string = "vikram"
# taking empty string
reverse_string = ""
# calculating the length of string
length = len(given_string)
# Traverse the strnum string in reverse order using for loop range function
for index in range(length-1, -1, -1):
    # add the character to reverse_string using string concatenation
    reverse_string = reverse_string+given_string[index]
# print the reversed string
print("The reversed string of", given_string, "=", reverse_string)

Output:

The reversed string of vikram = markiv

Method #4:Using list  and join functions

Approach:

  • Scan the given string.
  • Convert this string to list of characters using list() function.
  • Reverse the list using reverse() function
  • Join the list using join() function to get reversed string.
  • Print the reversed string.

Below is the implementation:

# given string
given_string = "vikram"
# converting to list of characters
stringlist = list(given_string)
# reverse the list
stringlist.reverse()
# convert this list to string using join
reverse_string = ''.join(stringlist)
# print the reversed string
print("The reversed string of", given_string, "=", reverse_string)

Output:

The reversed string of vikram = markiv

Method #5 : Using while loop

This method is similar to method #2(using for loop)

Approach: 

  • Scan the givenstring.
  • Take a empty string say revstring.
  • Take a variable length which gives the length of string.
  • Loop till length is greater than or equal to 0.
  • For each iteration
  • Add each character to revstring using string concatenation.
  • Decrement the length by 1.
  • print the revstring.

Below is the implementation:

# given string
given_string = "vikram"
# taking empty string
reverse_string = ""
# calculating the length of string
length = len(given_string)-1
# using while loop
while(length >= 0):
    # adding the character of string to reverse_string
    reverse_string = reverse_string+given_string[length]
    # decrement the length
    length = length-1
# print the reversed string
print("The reversed string of", given_string, "=", reverse_string)

Output:

The reversed string of vikram = markiv

These are methods to print the reversed string we can print directly by using loops and range() function but it doesn’t reverse the string it just print the reversed value of string.
Related Programs: