0% found this document useful (0 votes)
7 views6 pages

String - All in One

Uploaded by

ayushagrawal8408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

String - All in One

Uploaded by

ayushagrawal8408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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/stringDemo.py
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[-1:0:-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(s1.capitalize())
result
Python is a complete language
2. Find : s1.find('h')
3
Note : find first character index.
Page 2 of 6
3. isalnum : tell all values are alphanumeric

s1='12d34'
>>> s1.isalnum()
True
4. isdigit : tell all values are numeric
s1='1234'
>>> s1.isdigit()
True
5. isalpha
s2='aabc'
>>> s2.isalpha()
True
>>> s2='12aabc'
>>> s2.isalpha()
False
>>>
6. isspace : Find the space in string.
s1="My School"
>>> s1.isspace()
False
>>> s1=' '
>>> s1.isspace()
True
Example :
s1 = input("Enter First Name")
flg=True
for ch in s1:
if ch.isspace():
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'
>>> s1.islower()
True
>>> s2.islower();
False
8. isupper
>>> s1='wisdom'
>>> s2='SCHOOL'
>>> s1.isupper()
False
>>> s2.isupper()
True
>>>
9. istitle
>>> s1='python is a completer language'
>>> s1.istitle()
False
>>> s1='Python Is A Completer Language'
>>> s1.istitle()
True
>>>
10. lower
>>> s1='PYTHON'
>>> s1.lower()
'python'
>>>
11. upper
>>> s1='python'
>>> s1.upper()
'PYTHON'
>>>
12. title
s1='python is a completer language'
>>> s1.title()
'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 ch.isupper()==True:
upchar = True
if ch=='_' or ch=='@' :
spchar=True
if ch.isdigit() == 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

You might also like