How to reverse string in Python ?

Author: neptune | 16th-May-2022
#Python

Different ways to reverse String in Python.

String is a widely used datatype in Python just like a list. In most of the interviews, a question was asked How to reverse a string in Python?
In this article, we will see a different way to reverse a String in Python.

1. Using Extended Slice Syntax:

Extended slice method takes three parameters [ start index: end index: increment ] just like the for a loop.

Example:

testString = 'Neptuneworld'

testString=testString[::-1]

print(testString)

Output:


2.Using BuildIn method Reversed():

Reversed method returns a string reversed object which we can see that’s why we use to join and convert that object to a string using join.

Example :

testString = 'Neptuneworld'

testString=””.join(reversed(testString))

print(testString)

Output :


3. Using for loop:

In this method, we iterate the string and add the element to the start of initialized empty string.

Example:

testString = 'Neptuneworld'

revString = str()

for ch in testString:

revString = ch + revString

print(revString)

Output:

4.Using Recursive Function:

def reverse(testString):

if len(testString) == 0:

return testString

else:

return reverse(testString[1:]) + testString[0]

print(reverse("NeptuneWorld"))


5.Using List  :

Using list also we can reverse the string as shown below.

testString = 'Neptuneworld'

revString = []

for i in range(len(testString)-1,-1,-1):

revString.append(testString[i])


print(''.join(revString))


Thanks for Reading !!!





anonymous | June 23, 2022, 10:26 p.m.

What about Java Buildins?


anonymous | Sept. 20, 2021, 11:20 p.m.

✌️


anonymous | Sept. 20, 2021, 11:04 p.m.

Also write about Java



Related Blogs
How to extract Speech from Video using Python?
Author: neptune | 16th-Jun-2023
#Python #Projects
Simple and easy way to convert video into audio then text using Google Speech Recognition API...

How to download video from youtube using python module ?
Author: neptune | 15th-Jun-2023
#Python
We will let you know how you can easily download the Youtube high quality videos along with subtitle, thumbnail, description using python package..

Best Python package manager and package for virtual environment ?
Author: neptune | 18th-Jun-2023
#Python #Pip
We will explore the options of Pip, Virtualenv, Anaconda, and also introduce Pyenv as a helpful tool...

Deploy Django project on AWS with Apache2 and mod_wsgi module.
Author: neptune | 25th-May-2023
#Python #Django
In this blog I use the AWS Ubuntu 18.22 instance as Hosting platform and used Apache2 server with mod_wsgi for configurations. We create a django sample project then configure server...

Core Python Syllabus for Interviews
Author: neptune | 26th-Jul-2023
#Python #Interview
STRING MANIPULATION : Introduction to Python String, Accessing Individual Elements, String Operators, String Slices, String Functions and Methods...

Mostly asked Python Interview Questions - 2023.
Author: neptune | 30th-May-2023
#Python #Interview
Python interview questions for freshers. These questions asked in 2022 Python interviews...

Python Built-in functions lambda, map, filter, reduce.
Author: neptune | 15th-Jun-2023
#Python
We are going to explore in deep some important Python build-in functions lambda, map, filter and reduce with examples...

Python 3.9 new amazing features ?
Author: neptune | 26th-Jul-2023
#Python
Python 3.9 introduces new features such as dictionary union, string methods to remove prefixes and suffixes, type hinting, and speed improvements for built-in functions...

5 Languages that Replace Python with Proof
Author: neptune | 13th-Apr-2023
#Python
Julia, Rust, Go, Kotlin, and TypeScript are modern languages that could replace Python for specific use cases...

5 Best Python Testing Frameworks.
Author: neptune | 12th-Apr-2023
#Python #Testing
Python offers various testing frameworks, including Pytest, unittest, Nose, Robot Framework, and Behave, to build robust and reliable software...

10 Proven Ways to Earn Money Through Python
Author: neptune | 11th-Apr-2023
#Python
Python offers numerous earning opportunities from web development to teaching, data analysis, machine learning, automation, web scraping, and more...

Input and Output in Python
Author: neptune | 13th-Jul-2023
#Python
In this article, we will see how Python take input from user and How it display the output to user. First we cover input then output...

Monkey Patching in Python: A Powerful Yet Controversial Technique
Author: neptune | 01st-Aug-2023
#Python
Monkey patching in Python is a dynamic technique to modify code at runtime. It can add/alter behavior, but use it judiciously to avoid maintainability issues...

View More