L 3 - Strings

L 3 - Strings

***STRINGS ARE IMMUTABLE DATATYPE IN PYTHON ***

CONCATENATION OF STRING

8.png

STRING SLICING

How to get the character of a given index in a string in python

9.png

How to slice a given string

**SYNTAX - ** name_of_variable[ start : end : step ]

10.png

**NOTES - **

If nothing is written in place of start , then the function will start slicing from the index = 0

  • If nothing is written in place of end , then the function will do slicing till the end of the string.

  • If nothing is written in place of step , then the function will print every character.

NOTE -> WE CAN ONLY ACCESS THE CHARACTERS OF A STRING AND CANNOT CHANGE THE CHARACTER OF A STRING USING THIS FUNCTION ! IF YOU TRY DO SO THEN IT WILL SHOW ERROR AS SHOWN HERE

11.png

We are not able to do so because strings are immutable datatype in python.

STRING FUNCTIONS

len() function

This function returns the length of the string

12.png

name_of_string.endswith()

This function tells whether the variable string ends with the given string or not. *It returns a boolean *

13.png

name_of_string.count()

Counts the total no of occurence of any character.

14.png

name_of_string.capitalize()

It capitalizes the first letter of the given string.

**NOTE - ** It also makes all other letters (except first letter) in the string to lowercase.

15.png

name_of_string.find(word)

This function finds a word and returns the index of the first occurence of that word in the string.

16.png

name_of_string.replace(oldword,neword)

This function replaces the old word with the new word in the entire string.

17.png

lower () method

SYNTAX - name_of_string.lower()

USE - it converts all the letters of the string to lowercase .

a = 'GauRaV roHiLla'
a1 = a.lower()
print(a1)

b = 'Gau69raV RohIl96La 9669'    
b1 = b.lower()
print(b1)

c = '98989'
c1 = c.lower()
print(c1)
gaurav rohilla
gau69rav rohil96la 9669
98989

Comparison of two strings using lower() method

One of the common application of lower() method is to check if two strings are same or not.

a = 'gauRaV RohIllA'
b = 'gauraV ROhIlLA'

a1 = a.lower()
b1 = b.lower()
if(a1==b1):
    print('the two strings are the same')
else:
    print('the two strings are not the same')
the two strings are the same

upper () method

SYNTAX - name_of_string.upper()

USE - it converts all the letter of the string to uppercase

a = 'gauRaV RohIllA'
b = 'ga96uraV ROhI6l9LA 96969'
c = 'e3e3'
d = '9993'

a1 = a.upper()
b1 = b.upper()
c1 = c.upper()
d1 = d.upper()

print( a1)
print( b1)
print( c1)
print( d1)
GAURAV ROHILLA
GA96URAV ROHI6L9LA 96969
E3E3
9993

Comparison of two strings using upper () method

One of the common application of upper() method is to check if the two strings are same or not.

a = 'gauRaV RohIllA'
b = 'ga96uraV ROhI6l9LA 96969'

a1 = a.upper()
b1 = b.upper()

if(a1 == b1):
    print('the two strings are equal')
else:
    print('the two strings are not equal')
the two strings are not equal

All string methods return new values. They do not change the original string

You can see this in the examples given below.

17.1.png

17.2.png

17.3.png

\** SINCE STRING METHODS DO NOT CHANGE THE ORIGINAL STRING THUS IN THE ABOVE EXAMPLE WE NEED TO SAVE THE CHANGES AS , a = a.replace ('gaurav' , 'king') \**

ESCAPE SEQUENCE CHARACTER

The programmers refer to the "backslash ()" character as an escape character. In other words, it has a special meaning when we use it inside the strings. As the name suggests, the escape character escapes the characters in a string for a brief moment to introduce unique inclusion. That is to say; backlash signifies that the next character after it has a different meaning.

\ is the escape character in Python. Additionally, it announces that the next character has a different meaning. Moreover, the set of characters after , including it, is known as an escape sequence.

Escape sequence character comprises of more than one character but represents one character when used within the strings.

\n - New line escape sequence

\n means a new line , we will get a new line when we'll use this in the middle of the text in print function.

18.png

\t - tab space escape sequence

\t means a tab space

19.png

' and "- single quotes and double quotes escape sequence

' means single quote. " means double quote.

20.png

\ means , \ (a backslash)

If you want a backslash to be the part of your string then you cannot do it by typing \ in the string because \ is a escape sequence character in python. For doing so you will have to type \ . \ means \.

21.png