30 May Class With Chapter 2 String Handling
30 May Class With Chapter 2 String Handling
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
"""
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
"""
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.
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"]
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","")
board questions
1. split() or partition() => 1 mark question
2. string programs => text file question
3. string output => 3 mark