- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to reverse a string in Python?
String slicing and range operators can be used to reverse a string in Python. For example:
>>> 'Hello'[::-1] ‘olleH’ >>>‘Halloween’[::-1] ‘neewollaH’
The [] operator can take 3 numbers separated by colon ‘:’. The first is start index, second is end index and third is the stride. Here we have specified the stride as -1 and left other 2 empty, which means we want to go in reverse direction one at a time from beginning to end.
We can also reverse a string using a more readable but slower approach as follows:
>>> ''.join(reversed('Hello')) ‘olleH’ >>> ''.join(reversed('Halloween')) 'neewollaH'
We need to use join as reversed() returns a list and we need to reconstruct string from it.
- Related Articles
- How to reverse a string in Python program?
- Reverse String in Python
- Reverse Vowels of a String in Python
- Python Program to Reverse a String Using Recursion
- Reverse String II in Python
- Reverse words in a given String in Python
- How to quickly reverse a string in C++?
- How to reverse a given string in Java?
- How to reverse String in Java?
- How to reverse a String using C#?
- Python Program to Reverse a String without using Recursion
- How to reverse a number in Python?
- How to reverse a string using lambda expression in Java?
- How to Reverse a String in PL/SQL using C++
- How can I reverse a string in Java?

Advertisements