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

30 May Class With Chapter 2 String Handling

Uploaded by

lokeshprasaddr
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)
11 views6 pages

30 May Class With Chapter 2 String Handling

Uploaded by

lokeshprasaddr
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

Chapter 2 => list,tuple => over

String
=> Sequence of char is known as string.
s1="hello"
s2='hai'
s3="""welcome"""

s1="hello"
h e l l o
0 1 2 3 4

1)To access the string using for loop


solution:
s1="welcome"
for i in range(0,len(s1)):
print(s1[i])
for i in s1:
print(i)

"""
w
e
l
c
o
m
e
w
e
l
c
o
m
e
"""
string is immutable
=> can't change the value in a place
s1="hello"
s1[0]='a' ## invalid its immutable

String functions
1. isalpha()=> to check the given string is alphabet or not
2. isdigit() -> to check the given string is digit or not
3. isspace() - space or not
4. isalnum() - alphanumeric or not
5. islower()- lower case or not
6. isupper() - upper case or not
7. upper() - convert into upper case
8. lower() - convert into lower case
9. strip() =lstrip() rstrip()
10. split() -
11. partition()
12. index()
13. count()
solution:
s1="welcome"
print(s1.isalpha())
s2="good morning"
print(s2.isalpha())
s1="hello"
print(s1.isdigit())
"""
False
True
False
False

"""

1. write a python program to find the no of alphabets,


no of digits,no of spaces from the given string.

solution:
s1=input("enter the string")
a=0
d=0
s=0
spl=0
for i in s1:
if i.isalpha():
a=a+1
elif i.isdigit():
d=d+1
elif i.isspace():
s=s+1
else:
spl=spl+1
print("Alphabets=",a)
print("Digits=",d)
print("spaces=",s)
print("spl char=",spl)
"""
enter the stringwelcome to india 2024 %6
Alphabets= 14
Digits= 5
spaces= 4
spl char= 1
"""

tasks
2. Write a python program to find no of vowels,consonents
and alphabets in a given string.

3. write a python program to find the no of upper case,


lower case from the given string.

split() or partition() => 1 mark question

split()
=> splits a string into list of words(string) after
breaking the given string by the specified
separtor.
by default sep is space

example
s1="hello how are you"
print(s1.split())

output
["hello","how","are","you"]

using custom separator


s1="hello#how#are#you"
print(s1.split("#"))

output
["hello","how","are","you"]

Task
1. predict the output
s1="welcome to all"
print(s1.split('l'))

output
['we', 'come to a', '', '']

partition()
=> to split into words based on the
given separator
=> no default separator
=> it returns in a tuple
=. a tuple which contains only 3 element
=> first is word untile separtor
=> second is separtor
=> third word after separator.
example 1
s1="hello how are you"
print(s1.parition('o'))
output
("hell","o"," how are you")

example
s1="hello how are you"
print(s1.parition('u'))
output
("hello how are yo","u","")

1. write apython program to find the no


of words in a given string.
solution:
s1=input("enter the string")
count=0
l1=s1.split()
for word in l1:
count=count+1
print("no of words=",count)
"""
enter the stringhello how are you
no of words= 4
"""
2. write a python program to display the words
starts with 'h'
solution:
s1=input("enter the string")
l1=s1.split()
for word in l1:
if word[0]=='h' or word[0]=='H':
print(word)

3. Predict the output


mystr="No@1"
newstr=""
count=0
for i in mystr:
if count%2!=0:
newstr=newstr+str(count)
else:
if i.islower():
newstr=newstr+i.upper()
else:
newstr=newstr+i
count=count+1
print(newstr)

calc: mystr="No@1" count=0


i=N 0%2!=0 False newstr=N
i=o count=1 1%2!=0 newstr=N1
i=@ count=2 2%2!=0 False newstr=N1@
i=1 count=3 3%2!=0 True newstr=N1@3

string => overall concepts over

next class => dictionary

board questions
1. split() or partition() => 1 mark question
2. string programs => text file question
3. string output => 3 mark

You might also like