Class 11th Revision Tour
Intro, Strings,Lists
Tuples, Dictionaries, Sorting
Lectures by Harsh Sharma @ Apni Kaksha
Strings
Strings are collection of characters in contiguous memory locations.
e.g. 'HARSH'
H A R S H
Forward Backward
Indexing Indexing
a= 'Harsh'
print(a[0])
print(a[-2])
print(a)
a[1] = ‘A’
Strings are immutable. Hence, individual character can’t be changed.
Playing with strings
■ Adding 2 strings ( Concatenation )
a = 'Tinde'
b = 'Parle-G'
print(a+b)
■ Multiplying string ( Replication )
print(3 * 'ha')
print('Cringe' * 100)
Playing with string
■ Walking in a string ( Traversing )
a='Yalgaar!'
for ch in a:
print( ch,sep=‘x’)
■ Matching strings ( Membership )
'r' in 'romeo'
'J' in 'juliet'
'paani' in 'Japaani'
'Logic' in 'Reaction Videos'
Playing with string
■ Comparing strings ( Comparison )
'A' == 'A'
Comparison is done on the basis of
'A' == 'a' lexicographical order.(dictionary vaala)
'a' != 'A'
'abc' <= 'abc' Capital letters are considered smaller
'Abc' > 'abc' than small letters.
'A' < 'a'
Game of ASCII values
Function ord( ) takes a character and returns it’s ASCII value.
Function chr( ) takes an integer and returns the character of that ASCII value.
e.g. print( ord(‘A’) )
print( chr(65) )
String Slices
String Slice is a part of the string. We can slice a string this way:
A = 'Hello_World'
print( A[4:7]) will print 4,5 & 6th index
print( A[3: ] )
print( A[:8 ] )
print( A[4:1:-1] ) will print 4,3 & 2nd index in this order.
print( A[9:11] ) out of range will not be printed.
print( A[ : : ] ) whole will be printed.
String Functions
There are many inbuilt functions with which you can play with strings very easily.
Some of them are:
● a.capitalize( ) capitalize first letter.
● a.find(s, i, j) finds s between ith and jth index.
● a.isalpha( ), a.isdigit( ), a.isalnum( )
● a.isspace( ) true, if there are only space in string(at least one).
● a.islower( ), a.isupper( ), a.istitle( )
● a.lower( ), a.upper( ), a.title( )
● len(a)
Practice Time
Q1. Write a program that reads a line and prints :
Number of uppercase letters :
Number of lowercase letters :
Number of alphabets :
Number of digits :
Q2. Write a program that reads a string & checks whether it’s a palindrome
or not.