How to Reverse a String in Python
Tags: python, programming, codenewbie, tutorial
Published: 2021-12-13 16:13:44

---
title: How to Reverse a String in Python
published: true
date: 2021-12-12 12:16:02 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/how-to-reverse-a-string-in-python/
---

ItsMyCode |

Python has many functions for string manipulation. However, the Python string library does not have any built-in reverse() function to reverse a string.

## Reverse a string in Python

Let us look at the various other approaches to reverse a string in Python. Below are some of the approaches which can reverse the Python string.

1. Using slice notation
2. Using for loop
3. Using while loop
4. Using recursion
5. Using reversed() method

## Using extended slice operator

The easiest and fastest way to reverse a string in Python is using [**slice**](https://itsmycode.com/python-slice/)notation. The extended slice syntax is as follows.

```
string[start:stop:step]
```

The slice starts at the end of the string and traverses backwards with a step of -1 till it reaches position 0.

```python 
# Python reverse a string using extended slice operator

def reverse(str):   
    str = str[::-1]   
    return str  

text= "ItsMyCode"
print("The reversed string is :", reverse(text))
```

**Output**

```
The reversed string is : edoCyMstI

```

## Using for loop

The naive way to reverse a string is using the **`for`** loop. Each element in the string will be iterated using the `for` loop, and then it appends the characters to the beginning of a new string to obtain a reversed string.

```python 
# Python reverse a string using for loop

def reverse(s):
    str = ""
    for i in s:
        str = i + str
    return str

text = "ItsMyCode"
print("The reversed string is :", reverse(text))

```

**Output**

```python 
The reversed string is : edoCyMstI

```

## Using while loop

Like for loop, we can also use a `while` loop to reverse a string. We will determine the length of the string inside the while loop. We will join the characters from the end of the string and decrementing the count till it reaches 0.

```python 
# Python reverse a string using while loop

def reverse(s):
    str = ""
    count = len(s)
    while count > 0:
        str += s[count - 1] 
        count = count - 1 # decrement index
    return str

text = "ItsMyCode"
print("The reversed string is :", reverse(text))

```

**Output**

```python 
The reversed string is : edoCyMstI

```

## Using recursion

Recursion is a process where the function calls itself. In the code below, we will call a recursion method by passing a string as an argument to reverse the string. 

```python 
# Python reverse a string using recursion

def reverse(s):
    if len(s) == 0:
        return s
    else:
        return reverse(s[1:]) + s[0]

text = "ItsMyCode"
print("The reversed string is :", reverse(text))

```

**Output**

```python 
The reversed string is : edoCyMstI
```

## Using reversed() method

The **`reversed()`** returns the reversed iterator of the given string, and then its elements are joined empty string separated using **`join()`**. This approach will be the slowest approach when compared to the slicing approach.

```python 
# Python reverse a string using reversed

def reverse(str):
    str = "".join(reversed(str))
    return str

text = "ItsMyCode"
print("The reversed string is :", reverse(text))

```

**Output**

```python 
The reversed string is : edoCyMstI

```

The post [How to Reverse a String in Python](https://itsmycode.com/how-to-reverse-a-string-in-python/) appeared first on [ItsMyCode](https://itsmycode.com).