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([Link]())
s2="good morning"
print([Link]())
s1="hello"
print([Link]())
"""
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 [Link]():
a=a+1
elif [Link]():
d=d+1
elif [Link]():
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([Link]())
output
["hello","how","are","you"]
using custom separator
s1="hello#how#are#you"
print([Link]("#"))
output
["hello","how","are","you"]
Task
1. predict the output
s1="welcome to all"
print([Link]('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([Link]('o'))
output
("hell","o"," how are you")
example
s1="hello how are you"
print([Link]('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=[Link]()
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=[Link]()
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 [Link]():
newstr=newstr+[Link]()
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