PYTHON
STRINGS
Fill in the blanks
1. Collection of character is called String
2.String can be created by enclosing characters inside a single quote or double quotes.[T/F]. True.
3.a="flowers".print(a[4])= e
4.b="python".print(b[-3])=h
5. a="python".print(a[-4:5])= tho
6.String is immutable
7.We can delete or remove character from string .[T/F] .False
8.str1='python'.str2='world'.print(str1+str2)= pythonworld
9.x='python'.enum(x)=[(0,'p'),(1,'y'),(2,'t'),(3,'h'),(4,'o'),(5,'n')]
10.x=34.7868787.print('The value of x is %3.2f' %x). 34.78
11.s="hello world ".s.capitalize() = Hello World
12.s='I am an Indian'.s.count('an',0,len(str)) = 2
13. s='I am an Indian'.s.find('In',0,len(str)) = 8
14. s='I am an Indian'.s.replace('an','us') = I am us Indius.
15.a="Hello". a.isalpha()= True
16. a="Hello". a.isdigit()= False
17. a="welcome". a.upper() = WELCOME
18. a="WELCOME". a.lower() = welcome
19. a="WelCome". a.swapcase()= wELcOME
20. x=" Hi hello ". x.strip() = "Hi hello".
Answer the following questions
(10 marks)
1.Write a program for below string Method
§ capitalize()
§ center()
§ count()
§ find()
§ replace()
§ isalpha()
§ isdigit()
§ islower()
§ isnumeric()
§ isspace()
§ istitle()
§ isupper()
§ len()
§ lower()
§ upper()
§ lower()
§ strip()
§ swapcase()
§ split()
§ join()
captilize()
syntax
string.captilize()
>>>str="welcome
to traineetech "
>>>str.captilize()
Welcome
To Traineetech
center
syntax
string.center(width[,fillchar])
parameters
width
? this is the total width of the string.
fillchar
? this is the filler character.
for
example
>>>str1=" welcome
to traineetech "
>>>
str1.center(40,"#")
'######### welcome
to traineetech #########'
count()
syntax
string.count(substring,starindex=0,endindex=len(string))
for
example
>>>str="I
am an indian"
>>>str.count("an",0,len(str))
2
>>>
find()
>>>str="i
am an indian"
>>>str.count("an",0,len(str))
2
>>>str.find("an",0,len(str))
5
>>>str.find("am",0,len(str))
2
>>>str.find("um",0,len(str))
-1
Replace()
syntax
following
is the syntax for replace()method-
str.replace(old,new[,max])
for
example
>>>str="I
am an indian"
>>>str.replace("an","us")
'I
am us Indius'
isalpha()
>>>string="hai
"
>>>string.isalpha()
true
isdigit()
>>>a="1234"
>>>a.isdigit()
true
>>>
islower()
>>>
string=" welcome to traineetech "
>>>
string.islower()
True
isnumeric()
>>>
a="1234"
>>>a.numeric()
True
isspace()
>>>
string=" "
>>>
string.isspace()
True
>>>
istitle()
>>>
string=" Welcome To Traineetech "
>>>string.istitle()
True
>>>
string2=" WELCOME TO TRAINEETECH "
>>>string2.istitle()
False
>>>
isupper()
for
example
>>>string1="Hai"
>>>string1.upper()
'HAI'
lower()
>>>string1.lower()
'hai'
len(string)
>>>len(string1)
3
>>>
lstrip() - left strip(removes left space)
rstrip() - Right strip(removes right space)
strip() - removes all space
for
example
>>>string=" TRAINEETECH "
>>>string
' TRAINEETECH '
>>>string.lstrip()
' TRAINEETECH '
>>>string.rstrip()
' TRAINEETECH
'
>>>string.strip()
' TRAINEETECH '
>>>
upper()
for
example
>>>string="I
am an indian"
>>>string.upper()
I
AM AN INDIAN
swapcase()
>>>str="I
am an indian"
>>>str.swapcase()
'i
AM AN iNDIAN'
split()
>>>"This
will split all words into a list".split()
['This','will'.'split','all','words','into','a','list']
join()
>>>'
'.join(['This','will','join','all','words','into','a','string'])
'This
will join all words into a string'
>>>'Happy
New Year'.find('ew')
7
>>>'Happy
New Year',replace('Happy','Bright')
'Bright
New Year'
No comments:
Post a Comment