Strings
lege
Col
String is data type that stores a sequence of characters.
pna
A
USED THE BACK SLASH N (\n) TO PRINT THE STRING ON THE NEXT LINE BY USED THE BACK SLASH T (\t) WE CONVERT THE LINE INTO TAB SPACE LINE
Basic Operations
concatenation THE PROCESS OF ADDING THE TWO STRINGS IS CALLED THE
CONCATENATION
“hello” + “world” “helloworld”
length of str
len(str)
WE USED THIS FUNCTION TO CALCULATE THE LENGTH OF STRING
etc:
len(AWAIS) IS THE 5
IN THE LENGTH OF THE STRING THE EMPTY SPACED IS ALSO INCLUDED
Indexing INDEX MEANS THE POSITION OF THE STRINGS
IN THE INDEX THE EMPTY SPACES IS ALSO INCLUDED
Apna_College WITH THE USE OF THE INDEX WE PRINT ANY ONE CHARACTER
e
WE CAN EASILY ACCESS ANY CHARACTER
g
0 1 2 3 4 5 6 7 8 9 10 11
olle
na
str = “Apna_College”
C
Ap
str[0] is ‘A’, str[1] is ‘p’ ...
str[0] = ‘B’ #not allowed WE CANNOT ASSIGN THE VALUE TO THE ANY OTHER CHARACTER IN THE PROGRAMME
Slicing MEANS THE TAKING THE SLICE FROM THE YOUR STRING
Accessing parts of a string
str[ starting_idx : ending_idx ] #ending idx is not included
ge
str = “ApnaCollege”
str[ 1 : 4 ] is “pna”
olle
na C
str[ : 4 ] is same as str[ 0 : 4]
Ap
str[ 1 : ] is same as str[ 1 : len(str) ]
Slicing
Negative Index
ENDING INDEX IS ALSO NOT INCLUDED IN THE NEGATIVE SLICING OR INDEX
e
Apple
lleg
o
-5 -4 -3 -2 -1
na C
p
str = “Apple”
A
str[ -3 : -1 ] is “pl”
String Functions
str = “I am a coder.”
lege
[Link](“er.“) #returns true if string ends with substr
Col
a
[Link]( ) #capitalizes 1st char
Apn
[Link]( old, new ) #replaces all occurrences of old with new
[Link]( word ) #returns 1st index of 1st occurrence
[Link](“am“) #counts the occurrence of substr in string
Let‘s Practice
WAP to input user’s first name & print its length.
lege
Col
na
WAP to find the occurrence of ‘$’ in a String.
p
A
Conditional Statements
if-elif-else (SYNTAX)
ge
if(condition) :
Statement1
olle
a C
elif(condition):
n
Ap
Statement2
else:
StatementN
Conditional Statements
Grade students based on marks
ge
marks >= 90, grade = “A”
olle
C
90 > marks >= 80, grade = “B”
pna
80 > marks >= 70, grade = “C”
A
70 > marks, grade = “D”
Let‘s Practice
WAP to check if a number entered by the user is odd or even.
lege
Col
WAP to find the greatest of 3 numbers entered by the user.
pna
A
WAP to check if a number is a multiple of 7 or not.