 Contributed by –
Yash Monpara

## 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”