DAY 1 (STRING MANIPULATION)
TOPIC LEFT IN CLASS XI PORTION :
1. String
2. List
3. Tuples
4. Dictionaries
STRING : A string is a collection of characters. It is Immutable data Type.
Example : Python
It always kept in either ‘single’ code or “double”
0 1 2 3 4 5 Forward Index
P Y T H O N STRING
-6 -5 -4 -3 -2 -1 Backward index
Example1 :
s1 = "PYTHON"
print(s1)
================================ RESTART ================================
>>>
PYTHON
>>>
Example2 :
s1 = "PYTHON"
for ch in s1:
print(ch, end=' ')
================================ RESTART ================================
>>>
PYTHON
>>>
Example 3 : OF Forward indexing:
Another to print A String
s1 = "PYTHON"
n = len(s1)
print(“Length of S1 = ”, n)
for i in range (0,n):
print(s1[i], end=' *')
================================ RESTART ================================
>>>
P*Y*T*H*O*N*
>>>
Explain :
S1[i] = value
S1[0] = P
Page 1 of 6
S1[1] = Y
S1[2] = T
S1[3] = H
S1[4] = O
S1[5] = N
Example 3 : OF Reverse indexing:
s1 = "PYTHON"
n = len(s1)
print(“Length of S1 = ”, n)
for i in range (-1,(-n-1),-1): # -6-1 = -7
print(s1[i], end=' *')
RESTART: D:/01youtuber/Computer Science/xi/04 python/programs/[Link]
Length of S1 = 6
N *O *H *T *Y *P *
>>>
SLICING : It is used to slice a string into smaller parts.
Syntax :
String[Start:Stop, Step]
Example
s1= “Hello World”
print(s1[1::2])
output : 'el ol'
print(s1[-[Link]-1])
Output
'dlroW olle'
Some Important functions of String :
String is Class , and functions of class are always access by prefixing of dot operator.
What Is Class ? A class is collection of function.
What is object ? It is reference Variable which is used to access the function of class
What is the use of Dot (.) Operator ? It is used to provide accessibility of members inside the class
1. Capitalize : convert first character capitalize
s1 = 'python is a complete language'
print([Link]())
result
Python is a complete language
2. Find : [Link]('h')
3
Note : find first character index.
Page 2 of 6
3. isalnum : tell all values are alphanumeric
s1='12d34'
>>> [Link]()
True
4. isdigit : tell all values are numeric
s1='1234'
>>> [Link]()
True
5. isalpha
s2='aabc'
>>> [Link]()
True
>>> s2='12aabc'
>>> [Link]()
False
>>>
6. isspace : Find the space in string.
s1="My School"
>>> [Link]()
False
>>> s1=' '
>>> [Link]()
True
Example :
s1 = input("Enter First Name")
flg=True
for ch in s1:
if [Link]():
flg=False
if flg==True:
print("String has No Space")
else:
print("String has a Space")
OUTPUT
Enter First NameAnashin Bhardwaj
String has a Space
>>>
OUTPUT 2
Enter First NameAnashin
String has No Space
7. islower :
>>> s1='wisdom'
Page 3 of 6
>>> s2='SCHOOL'
>>> [Link]()
True
>>> [Link]();
False
8. isupper
>>> s1='wisdom'
>>> s2='SCHOOL'
>>> [Link]()
False
>>> [Link]()
True
>>>
9. istitle
>>> s1='python is a completer language'
>>> [Link]()
False
>>> s1='Python Is A Completer Language'
>>> [Link]()
True
>>>
10. lower
>>> s1='PYTHON'
>>> [Link]()
'python'
>>>
11. upper
>>> s1='python'
>>> [Link]()
'PYTHON'
>>>
12. title
s1='python is a completer language'
>>> [Link]()
'Python Is A Completer Language'
Home Work
A
bC
dEf
GhIj
KlMnO
Hint : islower, isupper, lower, upper, if, loop
List : Tomorrow we will discuss about list.
Page 4 of 6
Program :
Write a program in Language Python on the following basis
Given a word, Check whether if is is a valid password or not by using a regular expression. A password is
said to be “Good” if it formed by the following rules
i. Should begin with a letter
ii. Should contain atleast one digit and one special character(these are “@” , “_”)
iii. Atleast one character should be in upper case.
iv. Length of the password should be atleast 8.
Print “Bad” if the given word do not follow the above rules
s1 = input("Enter a String")
length = len(s1)
#print(length)
first = False
digit=False
spchar = False
upchar = False
if s1[0].isalpha()== True:
first = True
for ch in s1:
if [Link]()==True:
upchar = True
if ch=='_' or ch=='@' :
spchar=True
if [Link]() == True:
digit=True;
if spchar == True and upchar == True and first == True and digit == True and length>=8 :
print("Good")
else:
print ("Bad")
OUTPUT:
Enter a StringAnasinh@321
Good
OUTPUT 2 :
Enter a Stringanasinh321
Bad
Home Work : Write a program to count and prints its statistics as given below
Number of Upper Letters
Number of Lower Letters
Number of Alphabets
Number of digits
Number of Spaces.
Page 5 of 6
Page 6 of 6