Ticker

6/recent/ticker-posts

Strings in Python

 Strings in python:

A string is a sequence of characters. It is created by enclosing the characters inside a single quote, double quotes or even triple quotes. 

 

Strings in python

Triple quotes are used for multiline strings: 

 

Multiline strings in Python

Indexing and accessing the strings:

Unlike the other programming languages python indexing starts from ‘0’.  The string “WELCOME” is indexed in the following manner.

Indexing of Strings in Python


String = “WELCOME”

String[0]=W

String[1]=E

String[2]=L

String[3]=C

String[4]=O

String[5]=M

String[6]=E

 

Indexing of Strings

 

Indexing of strings

Slicing the strings:

Colon : operator is used to access or slice the substring from the string.

String[0:]=From 0th index till the end

String[:4]= From 0th index till the 3rd index. 

String[1:5]= From 1st index till the 4th index


Slicing the strings in Python


 Negative Slicing of the strings:

Negative slicing starts with the rightmost end character and its index is -1 and the next rightmost character is -2 and it continues like that.

Negative indexing of strings in python


String[-1]=Rightmost end character

String[-2]= Second rightmost end character

String[-5:-1]= From 1st index till the 4th index

 

Negative slicing of strings

Reversing the strings:

We can reverse the string by using [::-1]

 

Reversing the strings

Updating and deleting the strings:

The characters in the string cannot be replaced or deleted after assigning. But the whole string can be deleted using ‘del’ function and the whole string can be reassigned with a new string.

 

 

Updating the strings

Deleting the strings


String Operators:

‘+’  = Joins the string

‘*’  = Repeats the string

[ ] = Slicing operator

[:] = Range slicing operator

In = Membership operator

Not in = Non membership operator

 

String operators in python


Post a Comment

0 Comments