Dr.
DASARATHAN INTERNATIONAL SCHOOL, COIMBATORE – 17
(Affiliated to the Council for the Indian School Certificate Examinations, New Delhi)
Class IX Computer Worksheet
Chapter 15: Strings Data types in Python
String Declaration:
S=”python programming”
String functions:
1.Length function (len()) Output:
s="python programming" length of the string 18
print(“length of the string”,len(s))
2.Concatenation Output:
str1=’hello’ hello w0rld
str2="world" hellow0rld
str3=str1+' '+str2
print(str3)
str4=str1+str2
print(str4)
Extraction of string Output:
str="welcome" w
for i in str: e
print(i) l
c
o
m
e
s="vinodhini" Output:
print(s[0]) v
print(s[-3]) i
Lowercase function Output:
s="DASARATHAN" lowercase string:dasarathan
print("lowercase string:",s.lower())
st="dr.dasarathan international school" Output:
print(st.upper()) Converted to uppercase DR.DASARATHAN
INTERNATIONAL SCHOOL
Capitalize () function Output:
st="dr.dasarathan international school" First letter upper case
print("First letter upper Dr.dasarathan international school
case",st.capitalize())
Join() function Output:
st=['this','will','join', 'all','the','words']
print('*'.join(['this','will','join',
'all','the','words'])) this*will*join*all*the*words
st=['this','will','join', 'all','the','words']
print('#'.join(['this','will','join',
'all','the','words'])) this#will#join#all#the#words
st=['this','will','join', 'all','the','words']
print(' '.join(['this','will','join', this will join all the words
'all','the','words']))
st=['this','will','join', 'all','the','words']
print(join(['this','will','join',
'all','the','words']))
find() function Output:
print("vinodhini".find("k")) 5
Replicating the string Output:
str1="hello" Hellohellohello
print(str1*3)
String Slicing Output:
s="vinodhini" v
print(s[0]) h
print(s[5]) i
print(s[-3]) vinodhi
print(s[0:7]) vinodhini
print(s[:]) inodhini
print(s[1:])
str1="hello" Output:
print(str1*10) hellohellohellohellohellohellohellohellohellohello
Membership operator Output:
print("A" in "APCSP") True
False
print("R" in "APCSP") True
print("R" not in "APCSP")
s="Call" Output:
s1=s.replace('C','B') replaced string Ball
print("replaced string",s1)
Output:
new_var=4**3 64
print(new_var) 81
7
new_var=3**4
print(new_var)
new_var=3^4
print(new_var)
I. One Word:
1. How do you combine two strings?
Ans: String1 +String2
2. Which of the following is defined variable is a string?
cool_variable_1=23.18
cool_variable_2=9
cool_variable_3=”Important Message”
cool_variable_4=14**3
Ans: cool_variable_3=”Important Message”
3. How does one define a multiline string in a python?
Ans:Use triple quotes to declare the string in multiline.
“””
“””
4. What happens when the following code executed?
message=what a cool message!
print (message)
Ans: Python throws a Syntax error because the string is not surrounded by quotes.
5.______ allows you to put special characters into your code?
Ans: Escape Sequences
6.What will be the output be from the following code?
print("hello World!"*2)
Ans: hello world!hello world!
7. Joining the elements together to make a string is called what?
Ans: Concatenation
8.What would be the result?
people=['John','Rob','Bob']
(i)print(people[1])
(ii)print(people[4])
Ans:
(i)Rob
(ii)Traceback (most recent call last):
print(people[4])
IndexError: list index out of range
9. What letter will be printed on the screen after running the code?
city="Exeter"
letter=city[3]
print(letter)
Ans: t
10. Each item in a list has an address or index. The first index in a python list is what?
Ans: 0
11. What output will this code produce?
countries=["france","Wales","England"]
print(countries)
print(countries[1])
Ans: ['france', 'Wales', 'England'] Wales
12.What output will this code produce?
city="Exeter"
x=len(city)
print(x)
Ans: 6
13.What is printed by the following statements?
cheer="Go Eagles!"
print(cheer[0:1])
Ans: G
14. people=['john','Rob','Bob']
print(people[-1])
What will be result be?
Ans: Bob
15. What is printed in the following statements?
cheer="Go Eagles!"
print(cheer[5:8])
Ans: gle
16.What is printed in the following statemenst?
ss="Hola,friend"
print(ss.upper())
Ans: HOLA,FRIEND
17.what is printed by the following statements?
S=”python rocks”
Print(s[-3])
Ans:c
18.A datatype consisting of numbers, letters and symbols.
Ans: String
19.What is printed by the following statements:
fruit="banana"
bakedGood="nut bread"
print(fruit+bakedGood)
Ans: banananut bread
20.What is printed by the following statements?
s="python rocks"
print(s[7:11]*3)
Ans: rockrockrock
21. What is printed by the following statements?
singers="Peter,Paul and Mary"
print(singers[0:5])
Output: Peter
22. What is printed by the following code?
print("K" in "Kite")
print("R" in "Kite")
print("R" not in "Kite")
Ans: True
False
True
23. What happens If you run a following code?
foo=”a string”
Ans: The value “a string” is stored in the variable foo.
24. What will be the output for the following code?
print("abc DEF" .capitalize())
Ans: Abc def
25. Which operator will give the answer in integer?
Ans: //
26. What is the output :
example="helle"
example.find("e")
Ans: 1