VIDYA MANDIR PUBLIC SCHOOL, SEC-15A, FARIDABAD, HRY
CLASS: XI SUBJECT: COMPUTER SCIENCE
Chapter-4 STRING
ASSIGNMENT
1. What is the output of "hello"+1+2+3 ?
a) hello123 b) hello c) Invalid syntax d) hello6
2. What is the output of: print(len("Apple"))?
a) 5 b) 3 c) 4 d) 6
3. If x="AMAZING", the output of x[-5 : -1]
a) AMA b) AZIN c) AMAZ d) Invalid Syntax
4. What is the output of the following code
strn="Helps Heals Hurting"
c=0
for i in strn:
if (i=='H'):
c=c+1
print(c)
a) 2 b) 3 c) 4 d) None
5. Which method convert String "welcome to the beautiful world of python" into "Welcome
To The Beautiful World Of Python"
a) capitalize() b) upper() c) isupper() d) title()
6. Which operator is used for concatenation of strings?
a) * b) + c) & d) None
7. What function do you use to read a string?
a) input(“Enter a string”)
b) eval(input(“Enter a string”))
c) enter(“Enter a string”)
d) eval(enter(“Enter a string”))
8. Which of the following is False?
a) String is immutable.
b) String is enclosed by single('') or double quotes(" ")
c) In string, slicing is used to retrieve a subset of values. It just a substring.
d) None of the above
9. What is the output of the following code?
Str1="pynative"
print (str[1:3])
str2="Hello"
print(str*3)
10. What is the output of the following code
ex="snow world"
ex[3] = ‘s’
print(ex)
a) snow b) snow world c) snos world d) Error
11. What is the output of the following code
ex="bayrayrna"
ex.find("yr")
ex.index("yr")
"yr" in ex
ex.find("re")
ex.index("re")
"re" in ex
ex.replace("yr","t")
print(ex.count('a'))
12. What is the output of the following code
str1 = "Mysalary7000"
str2 = "7000"
str3="apple"
str4="My 123"
str5=" "
print(str1.isdigit())
print(str2.isdigit())
print(str1.isalnum())
print(str2.isalnum())
print(str3.isalnum())
print(str4.isalnum())
print(str1.isalpha())
print(str3.isalpha())
print(str3.isspace())
13. What is the output of the following code
x="welcome to my WORLD"
print(x.capitalize())
print(x.title())
print(x.upper())
print(x.lower())
SHORT ANSWERS
1. What is string ?
2. What is Traversing of string ?
3. What is a string slice ? How is it useful ?
4. For a string S1 storing ‘Goldy’ what would S1[0] , S1[-1] returns.
5. What are membership operators ? What do they basically do?
6. How is islower() function different from lower() function?
7. How is capitalize() function different from upper() function
8. What is the use of isupper( ) and isalnum( ) methods in python explains with example.
9. a="Hello" and a='Hello'. Does it makes any difference or is it same in python. Justify.
10. Find third occurrence of 'e' in sequence.
11. If a string , S1=”Loves One and All” Write the Python expression using string slices
a. ‘One an’
b. ‘d All ’
c. ‘AND’
d. ‘ Loves’
12.If a string , S2=”Loves One and All” . What would be returned by following string slices
a. S2[-7: -3]
b. S2[9:] + S2[:9]
c. S2 [10 : 12]
13.Find the output of following python code :
S=input("Enter the String :")
count =0
print(S)
while True:
if S[0] =='a':
S=S[2:]
elif S[-1]=='b':
S=S[:2]
else:
count=count+1
break
print(S, count)
What will be the output produced if input is (i) aabbcc (ii) aaccbb (iii) abcc
SHORT ANSWERS
1. What is string ?
It is a sequence of characters within single or double quotes (' ‘ , “ “ , )
2. What is Traversing of string ?
Traversing of string means accessing all the elements of the string one after the other using
index value.
3. What is a string slice ? How is it useful ?
A sub-part of a string is a string slice. The string slice with syntax string [ start : end : n ] is the
beginning at start and extending up to end but not including end , taking every n th element.
If s1 is a string , s1=”Welcome” , s1[2:5] will give ‘ lco’ .
If s2= “WelcomeIndia” , , s2[2:9:2] will give 'loen'
4. For a string S1 storing ‘Goldy’ what would S1[0] , S1[-1] returns.
S1=’Goldy ’
S1[0] = G , S1[ -1 ] = y
5. What are membership operators ? What do they basically do?
The in and not in are membership operators that test for the substring ’s presence in the string.
S1=”Hello”
< substring> in <String> E.g. 'ell' in S1 True
< substring> not in <String> E.g. "ell" not in S1 False
6. How is islower() function different from lower() function?
String. Islower() String.lower()
The islower() method returns True if all the Returns the string converted to lowercase.
character in the string are lowercase.
A="hello" B = "THERE"
B = "THERE" B. lower() "there"
A. islower () True
B. islower () False
7. How is capitalize() function different from upper() function ?
String.capitalize() String.upper()
Returns the string with its first character Returns the string converted to uppercase.
capitalized.
A="hello" A="hello"
A. capitalize() Hello A.upper() "HELLO"
8. What is the use of isupper( ) and isalnum( ) methods in python explains with example.
String. isupper( ) - The isupper() method returns True if all the characters are in upper case,
otherwise False.
E.g. A="hello"
B = "THERE"
A. isupper () False
B. isupper () True
String. isalnum( ) – The isalnum() return True if the character in the string are alphanumeric,
meaning alphabet letter (a-z) and numbers (0-9).
E.g. A="abc123" B = "1234" C="7000"
A. isalnum() True
B. isalnum() True
C. isalnum() True
9. a="Hello" and a='Hello'. Does it makes any difference or is it same in python. Justify.
No , Both are the way to declare string in Python
10. Find third occurrence of 'e' in sequence.
N1=‘sequence’.find(‘e’)
N2=‘sequence’.find(‘e’,N1+1)
N3=‘sequence’.find(‘e’,N2+1)
print(N3)
11. If a string , S1=”Loves One and All” Write the Python expression using string slices
e. ‘One an’
f. ‘d All ’
g. ‘AND’
h. ‘ Loves’
12.If a string , S2=”Loves One and All” . What would be returned by following string slices
a. S2[-7: -3]
b. S2[9:] + S2[:9]
c. S2 [10 : 12]
13.Find the output of following python code :
S=input("Enter the String :")
count =0
print(S)
while True:
if S[0] =='a':
S=S[2:]
elif S[-1]=='b':
S=S[:2]
else:
count=count+1
break
print(S, count)
What will be the output produced if input is (i) aabbcc (ii) aaccbb (iii) abcc