Browse by Domains

How To Reverse a String In Python | Different ways to Reverse a String in Python

Contributed by –
Yash Monpara

  1. What is a String?
  2. Reverse a String in Python code  
  3. How to convert list to String in Python 
  4. String functions in Python

What is a String?

We are going to learn the basic operations that are possible on a string, and the various options we can utilize. We will also look at some examples to understand these functions better. Before we begin, we need to understand that programming languages store strings as arrays. Therefore, we can always perform all the array or list operations on strings. For example, we can implement slicing, copying, reversal, and many more operations. Strings are arrays with a character data type.

Reverse a String

When we study languages such as C, we are provided with in-built functions to perform the reverse function. On the other hand, Python does not provide any in-built functions for the same. We need to use array slicing operation. 

Let us begin.

string=input(“Enter the string: “)
str_last_part = string[2:]
str_first_part = string[:3]

Thus, to obtain the reversed string we use the same concept. We make use of slicing, except for the fact that we read the string from the last character. That is accomplished by the following-

str_reverse = string[::-1]

The above line of code returns the reversed string. Consider the following list-

list_string= [“my”,”name”,”is”,”Lalith”]

Also read: Fibonacci Series in Python

How to convert list to String in Python

From the above list, how do I obtain a sentence, that is, how do I convert a list into a string?

We use an inbuilt function called join. This function concatenates all the individual strings present in the list to output a string.

''.join(list_string)

This gives a single string consisting of the following as the output-

my name is Lalith

On the contrary, if we had used the following command-

‘-’.join(list_string) 

We would have obtained this-

“-m-y- -n-a-m-e- -i-s- -L-a-l-I-t-h”

String functions in Python

Python is by far the most string friendly language out there. The degree of manipulation that python provides is enormous. Consider a few important functions from the string module in python. 

The string module contains several useful constants and classes, as well as deprecated legacy functions that are also available as methods on strings. Formatted strings use template strings or the % operator to obtain the output. Python also provides a module called re, which stands for regular expressions. Regular expressions will be covered in the coming tutorials.

For now let us experiment with a few of the functions that are available to us.

Let us consider the various string operations by looking at some examples. We will predefine some strings for further usage-

str1 = “ Kant is a great philosopher”
word = “fantastic”

MethodDescription
capitalize()Convert the beginning character to upper case
word.capitalize()
endswith()Returns true if the string ends with the specified value
if word.endswith(‘tic’):    Return True
find()Searches the string for a specified value and returns the first position of where it was foundword.find(‘a’) -> o/p =1
isalnum()Returns True if all characters in the string are alphanumericword.isalnum() ->True
isalpha()Returns True if all characters in the string are in the alphabetword.isalpha() ->True
isdecimal()Returns True if all characters in the string are decimalsword.isdecimal() -> False
isdigit()Returns True if all characters in the string are digitsWord.isdigit() -> False
isidentifier()Returns True if the string is an identifierword.isidentifier() -> True
islower()Returns True if all characters in the string are lower caseWord.islower() -> True
isnumeric()Returns True if all characters in the string are numericword.isnumeric() -> False
isspace()Returns True if all characters in the string are whitespacesword.isspace() ->True
istitle()Returns True if the string follows the rules of a titlestr1.istitle() -> False
isupper()Returns True if all characters in the string are upper caseWord.isupper() -> False
join()Joins the elements of an iterable to the end of the string”.join(str1) -> ‘ Kant is a great philosopher”-‘.join(str1) ->  ‘ -K-a-n-t- -i-s- -a- -g-r-e-a-t- -p-h-i-l-o-s-o-p-h-e-r’
lower()Converts a string into lower casestr1.lower() -> ‘ kant is a great philosopher’
split()Splits the string at the specified separator, and returns a liststr1.split()-> [‘Kant’, ‘is’, ‘a’, ‘great’, ‘philosopher’]
splitlines()Splits the string at line breaks and returns a liststr1.splitlines()-> [‘ Kant is a great philosopher’]
startswith()Returns true if the string starts with the specified valueword.startswith(‘fan’) -> True
title()Converts the first character of each word to upper casestr1.title() -> ‘ Kant Is A Great Philosopher’
upper()Converts a string into upper casestr1.upper() -> ‘ KANT IS A GREAT PHILOSOPHER’
zfill()Fills the string with a specified number of 0 values at the beginningstr=” # empty stringstr.zfill(10) ->’0000000000′

Also Read: Palindrome in Python

Thus, a majority of the functions that are of importance have been covered. Do experiment with natural language processing. The basics of natural language processing is pre-processing of data which is done with the help of regular expressions and string operations mentioned above. Stay tuned for more updates on regular expression.

Learn how to find the Factorial of a Number in Python

If you found this interesting and wish to learn more, upskill with Great Learning’s PGP – Artificial Intelligence and Machine Learning Course today.

Avatar photo
Great Learning Team
Great Learning's Blog covers the latest developments and innovations in technology that can be leveraged to build rewarding careers. You'll find career guides, tech tutorials and industry news to keep yourself updated with the fast-changing world of tech and business.

Leave a Comment

Your email address will not be published. Required fields are marked *

Great Learning Free Online Courses
Scroll to Top