HomeLinuxPython Reverse String

Python Reverse String


Being a Python programmer, there might be conditions the place there’s a have to reverse a string. There are a selection of the way to reverse a Python string, and on this Python weblog, we are going to discover among the commonest and environment friendly strategies.

Methodology 1: Reverse String Utilizing “String Slicing” in Python

The “String Slicing” strategy is essentially the most easy technique to reverse a specific Python string.

Syntax

 
Within the above syntax, “begin” and “cease” characterize the beginning and ending indices of the string, and “step” represents the increment or decrement worth.

Instance

The under code is used to reverse a string:

string = “Python Information”
reversed_string = string[::-1]
print(reversed_string)

 
On this code, set the “step” to “-1” to reverse a string using “slicing”. Utilizing this strategy, we begin from the final character of the actual string and transfer backward.

Output


Primarily based on the above snippet, the given string has been reversed utilizing the string-slicing technique.

Methodology 2: Reverse a String in Python by using the “reversed()” Perform

The “reversed()” operate might be utilized to reverse a specified Python string. This operate retrieves a reverse iterator, which we are able to convert to a string utilizing the “be part of()” technique.

Syntax

 
Right here, “sequence” is any iterable object similar to a tuple, string, checklist, or vary.

Instance

The under code is used to reverse a string:

string = “Python Information”
reversed_string = “”.be part of(reversed(string))
print(reversed_string)

 
Within the above code block, the “reversed()” operate reversed the string and the “be part of()” technique added the reversed string iterator worth to an empty string.

Output


Here’s a snippet that confirms the enter string is reversed.

Methodology 3: Reverse String in Python Using a “for” Loop

A “for” loop may also be utilized to reverse a Python string. On this strategy, we create a brand new string by appending every character from the unique string in reverse order as we iterate over it.

Instance

Let’s overview the next code:

str_1 = “Welcome to Python Information”
reversed_str_1 = “”
for character in str_1:
    reversed_str_1 = character + reversed_str_1
print(reversed_str_1)

 
Within the above code snippet, the “for” loop is utilized to iterate over the string and add it to the brand new empty string in reversed order.

Output


Primarily based on the above output, the given “string” has been reversed appropriately.

Methodology 4: Reverse String in Python Utilizing the “be part of()” Methodology and “Record Comprehension”

This technique makes use of “Record Comprehension” to reverse a string after which makes use of the “be part of()” technique to unite the characters.

Syntax of the “be part of()” Methodology

 
Within the above syntax, “string” corresponds to the separator that joins the weather of the “iterable”, which could be a checklist, tuple, string, and many others.

Syntax of “Record Comprehension”

[expression for item in iterable if condition]

 
Instance

Right here is an instance code:

str_1 = “Welcome to Python Information”
reversed_string = “”.be part of([str_1[i] for i in vary(len(str_1)1, –1, –1)])
print(reversed_string)

 
Within the above code block, the “Record Comprehension” strategy is used with the mixture of the “be part of()” technique to reverse the string and add it to the brand new empty string.

Output

Methodology 5: Reverse String in Python Utilizing the “Recursion” Perform

Recursion” is one other technique that may be employed to reverse a specific string. On this technique, the reversed string is returned by an outlined operate that accepts a singular string as an argument/parameter.

Instance

Here’s a code block that’s utilized to reverse a Python string:

def rev_string(str_1):
    if len(str_1) == 0:
        return str_1
    else:
        return rev_string(str_1[1:]) + str_1[0]
str_1 = “Python Information”
print(rev_string(str_1))

 
Right here, the user-defined recursive operate named “rev_string()” is utilized to the given string to reverse it within the backward path by passing the initialized string as its argument.

Output


The above snippet verified that the given string has been reversed.

Methodology 6: Reverse String in Python Utilizing the “functools.cut back()” Perform

Within the “functools” module, the operate “cut back()” may also be utilized to reverse a Python string. This technique defines a “lambda” operate that reverses the order of two arguments. A lambda operate is then utilized to the string utilizing the “cut back()” operate.

Syntax

functools.cut back(operate, iterable[, initializer])

 
This operate takes an iterable and a operate that operates on two arguments. It executes the operate to the primary two gadgets/component of the iterable, then to the output and the following component, and so forth, till there is just one worth left. The non-compulsory argument “initializer” supplies the preliminary worth for the buildup. If it isn’t given, then the primary merchandise/component of the iterable is utilized because the preliminary worth.

Instance

Overview of the under Python code block:

from functools import cut back
str_1 = “Python Information”
reversed_str_1 = cut back(lambda x, y: y + x, str_1)
print(reversed_str_1)

 
Within the above code, the “cut back()” operate takes the lambda operate and given string as its arguments, respectively, and retrieves the reversed string.

Output


This end result implies that the given string has been reversed accordingly.

Conclusion

The “String Slicing”, “reversed()” operate, “for” Loop, “be part of()” technique, “Record Comprehension”, or the “functools.cut back()” operate can be utilized to reverse the string in Python. Nonetheless, the “String Slicing” strategy is really helpful because it is likely one of the easiest methods to use the specified performance. This information introduced other ways to reverse a Python string.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments