0% found this document useful (0 votes)
11 views50 pages

Revision-String Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views50 pages

Revision-String Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

‭Multiple Choice Questions‬

‭Question 1‬
‭Negative index -1 belongs to .......... of string.‬

‭ .‬ ‭first character‬
1
‭2.‬ ‭last character ✓‬
‭3.‬ ‭second last character‬
‭4.‬ ‭second character‬

‭Question 2‬
‭Which of the following is/are not legal string operators?‬

‭ .‬ ‭in‬
1
‭2.‬ ‭+‬
‭3.‬ ‭*‬
‭4.‬ ‭/ ✓‬

‭Question 3‬
‭ hich of the following functions will return the total number of characters‬
W
‭in a string?‬

‭ .‬ ‭count()‬
1
‭2.‬ ‭index()‬
‭3.‬ ‭len() ✓‬
‭4.‬ ‭all of these‬

‭Question 4‬
‭ hich of the following functions will return the last three characters of a‬
W
‭string s?‬

‭1.‬ ‭s[3:]‬
‭ .‬ ‭s[:3]‬
2
‭3.‬ ‭s[-3:] ✓‬
‭4.‬ ‭s[:-3]‬

‭Question 5‬
‭ hich of the following functions will return the first three characters of a‬
W
‭string s?‬

‭ .‬ ‭s[3:]‬
1
‭2.‬ ‭s[:3] ✓‬
‭3.‬ ‭s[-3:]‬
‭4.‬ ‭s[:-3]‬

‭Question 6‬
‭Which of the following functions will return the string in all caps?‬

‭ .‬ ‭upper() ✓‬
1
‭2.‬ ‭toupper()‬
‭3.‬ ‭isupper()‬
‭4.‬ ‭to-upper()‬

‭Question 7‬
‭ hich of the following functions will return the string with every 'P'‬
W
‭replaced with a 'z'?‬

‭ .‬ ‭find()‬
1
‭2.‬ ‭index()‬
‭3.‬ ‭replace() ✓‬
‭4.‬ ‭split()‬

‭Question 8‬
‭ hich of the following functions will return a list containing all words of‬
W
‭the string?‬
‭ .‬ ‭find()‬
1
‭2.‬ ‭index()‬
‭3.‬ ‭partition()‬
‭4.‬ ‭split() ✓‬

‭Question 9‬
‭Which of the following functions will always return a tuple of 3 elements?‬

‭ .‬ ‭find()‬
1
‭2.‬ ‭index()‬
‭3.‬ ‭partition() ✓‬
‭4.‬ ‭split()‬

‭Question 10‬
‭What is the output of the following code?‬
‭tr1‬‭
s =‬‭
"Mission 999"‬
str2‬‭
‭ =‬‭
"999"‬
print‬
‭ (str1.‬
‭ isdigit‬
‭ (),str2.‬
‭ isdigit‬
‭ ())‬

‭ .‬ ‭False True ✓‬
1
‭2.‬ ‭False False‬
‭3.‬ ‭True False‬
‭4.‬ ‭True True‬

‭Question 11‬
‭Choose the correct function to get the ASCII code of a character.‬

‭ .‬ ‭char('char')‬
1
‭2.‬ ‭ord('char') ✓‬
‭3.‬ ‭ascii('char')‬
‭4.‬ ‭All of these‬

‭Question 12‬
‭ hich method should I use to convert String "Python programming is‬
W
‭fun" to "Python Programming Is Fun" ?‬

‭ .‬ ‭capitalize()‬
1
‭2.‬ ‭title() ✓‬
‭3.‬ ‭istitle()‬
‭4.‬ ‭upper()‬

‭Question 13‬
‭Guess the correct output of the following String operations.‬
‭tr1‬‭
s =‬‭
'Wah'‬
print‬
‭ (str1‬
‭ *2‬
‭ )‬

‭ .‬ ‭WahWah ✓‬
1
‭2.‬ ‭TypeError: unsupported operand type(s) for * : 'str' and 'int'‬
‭3.‬ ‭WWaahh‬
‭4.‬ ‭Wah2‬

‭Question 14‬
‭What is the output of the following string operation?‬
‭tr‬‭
s =‬‭"My roll no. is 12"‬
print‬
‭ (str.‬
‭ isalnum‬
‭ ())‬

‭ .‬ ‭True‬
1
‭2.‬ ‭False ✓‬
‭3.‬ ‭Error‬
‭4.‬ ‭No output‬

‭Question 15‬
‭Select the correct output of the following String operations.‬
‭tr1‬‭
s =‬‭
'Waha'‬
print‬
‭ (str1[:‬
‭ 3‭
‭]‬‬‭
+‬‭
'Bhyi'‬‭
+‬‭
str1[‬
-3‬
‭ :])‬

‭ .‬ ‭Wah Bhyi Wah‬
1
‭2.‬ ‭WahBhyiaha ✓‬
‭3.‬ ‭WahBhyiWah‬
‭4.‬ ‭WahBhyiWaha‬

‭Question 16‬
‭Select the correct output of the following String operations.‬
‭tr‬‭
s =‬‭"my name is Anu John"‬
print‬
‭ (str.‬
‭ capitalize‬
‭ ())‬

‭ .‬ ‭'My name is anu john' ✓‬


1
‭2.‬ ‭TypeError: unsupported operand type(s) for * : 'str' and 'int'‬
‭3.‬ ‭'My name is Anu John'‬
‭4.‬ ‭'My Name Is Anu John'‬

‭Question 17‬
‭Choose the correct function to get the character from ASCII number.‬

‭ .‬ ‭ascii(number)‬
1
‭2.‬ ‭char(number)‬
‭3.‬ ‭chr(number) ✓‬
‭4.‬ ‭all of these‬

‭Question 18‬
‭s = ' '(single space). Then s.isalnum() will return.‬

‭ .‬ ‭True‬
1
‭2.‬ ‭False ✓‬
‭3.‬ ‭Error‬
‭4.‬ ‭nothing‬

‭Question 19‬
‭ hich of the following functions removes all leading and trailing spaces‬
W
‭from a string?‬

‭ .‬ ‭lstrip()‬
1
‭2.‬ ‭rstrip()‬
‭3.‬ ‭strip() ✓‬
‭4.‬ ‭all of these‬

‭Question 20‬
‭ hich of the following functions will raise an error if the given substring‬
W
‭is not found in the string?‬

‭ .‬ ‭find()‬
1
‭2.‬ ‭index() ✓‬
‭3.‬ ‭replace()‬
‭4.‬ ‭all of these‬

‭Fill in the Blanks‬

‭Question 1‬
‭The string indexes begin‬‭0‬‭onwards.‬

‭Question 2‬
‭For strings,‬‭+‬‭operator performs concatenation.‬

‭Question 3‬
‭For strings,‬‭*‬‭operator performs replication.‬

‭Question 4‬
‭The‬‭in‬‭and‬‭not in‬‭are membership operators for strings‬‭(in, not in).‬

‭Question 5‬
‭The‬‭ord()‬‭returns the ASCII value of a given character.‬

‭Question 6‬
‭If a string contains letters and digits, function‬‭isalnum()‬‭will return true.‬

‭Question 7‬
‭'ab'.isalpha() will return value as‬‭True‬‭.‬

‭Question 8‬
‭To get each word's first letter capitalized,‬‭title()‬‭function is used.‬

‭Question 9‬
‭Function‬‭index()‬‭raises an exception if the substring‬‭is not found.‬

‭Question 10‬
‭Function‬‭split()‬‭divides a line of text into individual‬‭words.‬

‭True/False Questions‬

‭Question 1‬
‭ trings have both positive and negative indexes.‬
S
‭True‬

‭Question 2‬
‭ ython does not support a character type; a single character is treated‬
P
‭as strings of length one.‬
‭True‬

‭Question 3‬
‭ trings are immutable in Python, which means a string cannot be‬
S
‭modified.‬
‭True‬

‭Question 4‬
‭ ike '+', all other arithmetic operators are also supported by strings.‬
L
‭False‬

‭Question 5‬
‭ unctions capitalize() and title() return the same result.‬
F
‭False‬

‭Question 6‬
‭ unctions partition() and split() work identically.‬
F
‭False‬

‭Question 7‬
‭ he find() and index() are similar functions.‬
T
‭True‬

‭Question 8‬
‭ he find() does not raise an exception if the substring is not found.‬
T
‭True‬

‭Question 9‬
‭ he partition() function's result is always a 3-element tuple.‬
T
‭True‬

‭Question 10‬
‭ he split() returns always a 3-element list.‬
T
‭False‬

‭ ype A : Short Answer Questions/Conceptual‬


T
‭Questions‬

‭Question 1‬
‭ rite a Python script that traverses through an input string and prints its‬
W
‭characters in different lines — two characters per line.‬

‭Answer‬
‭tr‬‭
s =‬‭
input‬(‭
‭"‬Enter the string: "‬)‬

length‬‭
‭ =‬‭
len‬
(str)‬

for‬‭
‭ a‬‭
in‬‭range‬(‭
‭0‬‭
,
‬ length,‬‭
2‭
)‬:‬
print‬
‭ (str[a:a‬
‭ +2‬
‭ ])‬

‭Output‬
‭nter the string: KnowledgeBoat‬
E
Kn‬

ow‬

le‬

dg‬

eB‬

oa‬

t‬

‭Question 2‬
‭ ut of the following operators, which ones can be used with strings in‬
O
‭Python?‬

‭=, -, *, /, //, %, >, <>, in, not in, <=‬

‭Answer‬
‭The following Python operators can be used with strings:‬

‭=, *, >, in, not in, <=‬

‭Question 3‬
‭What is the result of following statement, if the input is 'Fun'?‬

‭print(input("...") + "trial" + "Ooty" * 3)‬

‭Answer‬

‭The result of the statement is:‬

‭FuntrialOotyOotyOoty‬

‭Question 4‬
‭Which of the following is not a Python legal string operation?‬

(‭ a) 'abc' + 'abc'‬
‭(b) 'abc' * 3‬
‭(c) 'abc' + .3‬
‭(d) 'abc.lower()‬

‭Answer‬

'‭abc' + .3 is not a legal string operation in Python. The operands of +‬


‭operator should be both string or both numeric. Here one operand is‬
‭string and other is numeric. This is not allowed in Python.‬

‭Question 5‬
‭Can you say strings are character lists? Why? Why not?‬

‭Answer‬

‭ trings are sequence of characters where each character has a unique‬


S
‭index. This implies that strings are iterable like lists but unlike lists they‬
‭ re immutable so they cannot be modified at runtime. Therefore, strings‬
a
‭can't be considered as character lists. For example,‬
‭tr‬‭
s =‬‭'cat'‬
# The below statement‬

# is INVALID as strings‬

# are immutable‬

str[‬
‭ 0‬
‭ ]‬‭
‭ =‬‭
'b'‬

‭ Considering character lists‬


#
strList‬‭
‭ =‬‭[‭
'
‬c'‬,‬‭
‭ 'a'‬
,‬‭
‭ 't'‬
]‬

# The below statement‬

# is VALID as lists‬

# are mutable‬

strList[‬
‭ 0‭
‭]‬‬‭
=‬‭
'b'‬

‭Question 6‬
‭ iven a string S = "CARPE DIEM". If n is length/2 (length is the length of‬
G
‭the given string), then what would following return?‬

(‭ a) S[: n]‬
‭(b) S[n :]‬
‭(c) S[n : n]‬
‭(d) S[1 : n]‬
‭(e) S[n : length - 1]‬

‭Answer‬

(‭ a) CARPE‬
‭(b) DIEM‬
‭(c) (Empty String)‬
‭(d) ARPE‬
‭(e) DIE‬

‭Question 7‬
‭ rom the string S = "CARPE DIEM", which ranges return "DIE" and‬
F
‭"CAR"?‬

‭Answer‬

‭ .‬ ‭S[6:9] returns DIE‬


1
‭2.‬ ‭S[:3] returns CAR‬

‭Question 8‬
‭ hat happens when from a string slice you skip the start and/or end‬
W
‭values of the slice?‬

‭Answer‬

I‭f start value is skipped, it is assumed as 0 i.e. the slice begins from the‬
‭start of the string.‬

I‭f end value is skipped, it is assumed as the last index of the string i.e.‬
‭the slice extends till the end of the string.‬

‭Question 9‬
‭What would the following expressions return?‬

‭ .‬ ‭"Hello World".upper( ).lower( )‬


1
‭2.‬ ‭"Hello World".lower( ).upper( )‬
‭3.‬ ‭"Hello World".find("Wor", 1, 6)‬
‭4.‬ ‭"Hello World".find("Wor")‬
‭5.‬ ‭"Hello World".find("wor")‬
‭6.‬ ‭"Hello World".isalpha( )‬
‭7.‬ ‭"Hello World".isalnum( )‬
‭8.‬ ‭"1234".isdigit( )‬
‭9.‬ ‭"123FGH".isdigit( )‬

‭Answer‬

‭1.‬ ‭hello world‬


‭ .‬ ‭HELLO WORLD‬
2
‭3.‬ ‭-1‬
‭4.‬ ‭6‬
‭5.‬ ‭-1‬
‭6.‬ ‭False‬
‭7.‬ ‭False‬
‭8.‬ ‭True‬
‭9.‬ ‭False‬

‭Explanation‬

‭1.‬ ‭upper() first converts all letters of "Hello World" to uppercase. Then‬
‭"HELLO WORLD".lower() converts all letters to lowercase.‬
‭2.‬ ‭lower() first converts all letters of "Hello World" to lowercase. Then‬
‭"hello world".upper() converts all letters to uppercase.‬
‭3.‬ ‭"Hello World".find("Wor", 1, 6) searches for the presence of‬
‭substring "Wor" between 1 and 6 indexes of string "Hello World".‬
‭Substring from 1 to 6 index is "ello W". As "Wor" is not present in‬
‭this hence the result is False.‬
‭4.‬ ‭"Hello World".find("Wor") searches for the presence of substring‬
‭"Wor" in the entire "Hello World" string. Substring "Wor" starts at‬
‭index 6 of "Hello World" hence the result is 6.‬
‭5.‬ ‭"Hello World".find("wor") searches for the presence of substring‬
‭"wor" in the entire "Hello World" string. find() performs case‬
‭sensitive search so "wor" and "Wor" are different hence the result‬
‭is -1.‬
‭6.‬ ‭"Hello World".isalpha( ) checks if all characters in the string as‬
‭alphabets. As a space is also present in the string hence it returns‬
‭False.‬
‭7.‬ ‭"Hello World".isalnum( ) checks if all characters in the string are‬
‭either alphabets or digits. As a space is also present in the string‬
‭which is neither an alphabet nor a string hence it returns False.‬
‭8.‬ ‭"1234".isdigit( ) checks if all characters in the string are digits or‬
‭not. As all characters are digits hence the result is True.‬
‭9.‬ ‭As "FGH" in the string "123FGH" are not digits hence the result is‬
‭False.‬

‭Question 10‬
‭ hich functions would you choose to use to remove leading and trailing‬
W
‭white spaces from a given string?‬

‭Answer‬

l‭strip() removes leading white-spaces, rstrip() removes trailing‬


‭white-spaces and strip() removes leading and trailing white-spaces from‬
‭a given string.‬

‭Question 11‬
‭ ry to find out if for any case, the string functions isalnum( ) and isalpha(‬
T
‭) return the same result‬

‭Answer‬

‭isalnum( ) and isalpha( ) return the same result in the following cases:‬

‭1.‬ ‭If string contains only alphabets then both isalnum( ) and isalpha( )‬
‭return True. For example, "Hello".isalpha() and "Hello".isalnum()‬
‭return True.‬
‭2.‬ ‭If string contains only special characters and/or white-spaces then‬
‭both isalnum( ) and isalpha( ) return False. For example,‬
‭"*#".isalpha() and "*#".isalnum() return False.‬

‭Question 12‬
‭Suggest appropriate functions for the following tasks:‬

‭ .‬ ‭To check whether the string contains digits‬


1
‭2.‬ ‭To find for the occurrence a string within another string‬
‭3.‬ ‭To convert the first letter of a string to upper case‬
‭ .‬ ‭to capitalize all the letters of the string‬
4
‭5.‬ ‭to check whether all letters of the string are in capital letters‬
‭6.‬ ‭to remove from right of a string all string-combinations from a given‬
‭set of letters‬
‭7.‬ ‭to remove all white spaces from the beginning of a string‬

‭Answer‬

‭ .‬ ‭isdigit()‬
1
‭2.‬ ‭find()‬
‭3.‬ ‭capitalize()‬
‭4.‬ ‭upper()‬
‭5.‬ ‭isupper()‬
‭6.‬ ‭rstrip(characters)‬
‭7.‬ ‭lstrip()‬

‭Question 13‬
‭In a string slice, the start and end values can be beyond limits. Why?‬

‭Answer‬

‭ tring slicing always returns a subsequence and empty subsequence is‬


S
‭a valid sequence. Thus, when a string is sliced outside the bounds, it still‬
‭can return empty subsequence and hence Python gives no errors and‬
‭returns empty subsequence.‬

‭Question 14‬
‭ an you specify an out of bound index when accessing a single‬
C
‭character from a string? Why?‬

‭Answer‬

‭ e cannot specify an out of bound index when accessing a single‬


W
‭character from a string, it will cause an error. When we use an index, we‬
‭are accessing a constituent character of the string. If the index is out of‬
‭ ounds there is no character to return from the given index hence‬
b
‭Python throws string index out of range error.‬

‭Question 15‬
‭Can you add two strings? What effect does ' + ' have on strings?‬

‭Answer‬

‭ es two strings can be added using the '+' operator. '+' operator‬
Y
‭concatenates two strings.‬

‭Type B: Application Based Questions‬

‭Question 1a‬
‭What is the result of the following expression?‬
‭rint‬
p (‭
‭"‬""‬
1‬

2‬

3‬

"""‬
‭ )‬

‭Answer‬

1‬

2‬

3‬

‭Question 1b‬
‭What is the result of the following expression?‬
‭ext‬‭
t =‬‭"Test.\nNext line."‬
print‬‭
‭ (text)‬
‭Answer‬
‭est.‬
T
Next line.‬

‭Question 1c‬
‭What is the result of the following expression?‬
‭rint‬‭
p (‬'One'‬
‭ ,‬‭
‭ ' Two '‬‭
*‬‭
2‭
)‬‬
print‬‭
‭ (‬'One '‬‭
‭ +‬‭
'Two'‬‭
*‬‭
2‭
)‬‬
print‬‭
‭ (‬len‬
‭ (‭
‭ '
‬10123456789'‬
))‬

‭Answer‬
‭ne Two Two‬
O
One TwoTwo‬

11‬

‭Question 1d‬
‭What is the result of the following expression?‬
‭‬‭
s =‬‭
'0123456789'‬
print‬
‭ (s[‬
‭ 3‭
‭]‬,‬‭
", "‬
, s[‬
‭ 0‬‭
‭ :‬‭3‭
]‬,‬‭" - "‬
, s[‬
‭ 2‬‭
‭ :‬‭
5‬])‬

print‬
‭ (s[:‬
‭ 3‬
‭ ],‬‭
‭ " - "‬, s[‬
‭ 3‭
‭:‬],‬‭ ", "‬
, s[‬
‭ 3‭
‭:‬‭
1
‬00‬
])‬

print‬
‭ (s[‬
‭ 20‬
‭ :], s[‬
‭ 2‭
‭:
‬‬1‭
‭]‬, s[‬1‭
‭:‬‬
1‭
‭ ]
‬)‬

‭Answer‬
‭ ,
3 012 - 234‬
012
‭ - 3456789 , 3456789‬

‭Question 1e‬
‭What is the result of the following expression?‬
‭‬‭
s =‬'987654321'‬

print‬‭
‭ (s[‬
-1‬
‭ ], s[‬
‭ -3‬
‭ ])‬

print‬‭
‭ (s[‬
-3‬
‭ :], s[:‬
‭ -3‬
‭ ])‬

print‬‭
‭ (s[‬
-100‬
‭ :‬
‭ -3‬
‭ ], s[‬
‭ -100‬
‭ :‬
‭ 3‭
‭]‬)‬

‭Answer‬
‭ 3‬
1
321 987654‬

987654 987‬

‭Question 2a‬
‭What will be the output produced by following code fragments?‬
‭‬‭
y =‬‭
str‬(‭
‭1‬23‬
)‬

x‬‭
‭ =‬‭
"hello"‬‭ *‬‭
3‬
print‬‭
‭ (x, y)‬
x‬‭
‭ =‬‭
"hello"‬‭ +‬‭
"world"‬
y‬‭
‭ =‬‭
len‬(x)‬

print‬‭
‭ (y, x)‬

‭Answer‬

‭Output‬

‭ellohellohello 123‬
h
10 helloworld‬

‭Explanation‬

s‭ tr(123) converts the number 123 to string and stores in y so y becomes‬


‭"123". "hello" * 3 repeats "hello" 3 times and stores it in x so x becomes‬
‭"hellohellohello".‬

"‭ hello" + "world" concatenates both the strings so x becomes‬


‭"helloworld". As "helloworld" contains 10 characters so len(x) returns 10.‬
‭Question 2b‬
‭What will be the output produced by following code fragments?‬
‭‬‭
x =‬‭
"hello"‬‭+‬‭
\‬
"to Python"‬‭
‭ +‬‭
\‬
"world"‬

for‬‭
‭ char‬‭
in‬‭x :‬
y‬‭
‭ =‬‭
char‬
print‬‭
‭ (y,‬‭
' : '‬
, end‬‭
‭ =‬‭
' '‬
)‬

‭Answer‬

‭Output‬


h : e : l : l : o : t : o : : P : y : t :‬
h
‭ : o : n : w : o : r : l : d :‬

‭Explanation‬

I‭nside the for loop, we are traversing the string "helloto Pythonworld"‬
‭character by character and printing each character followed by a colon‬
‭(:).‬

‭Question 2c‬
‭What will be the output produced by following code fragments?‬
‭‬‭
x =‬‭
"hello world"‬
print‬‭
‭ (x[:‬2‭
‭]‬, x[:‬-2‬
‭ ], x[‬
‭ -2‬
‭ :])‬

print‬‭
‭ (x[‬
6‬
‭ ], x[‬
‭ 2‭
‭:‬‭
4
‬‬])‬

print‬‭
‭ (x[‬
2‬
‭ :‭
‭-‬3‬
], x[‬
‭ -4‬
‭ :‭
‭-‬2‬
])‬

‭Answer‬

‭Output‬

‭e hello wor ld‬


h
w ll‬

llo wo or‬

‭Explanation‬

x‭ [:2] ⇒ he‬
‭x[:-2] ⇒ hello wor‬
‭x[-2:] ⇒ ld‬

x‭ [6] ⇒ w‬
‭x[2:4] ⇒ ll‬

x‭ [2:-3] ⇒ llo wo‬


‭x[-4:-2] ⇒ or‬

‭Question 3‬
‭ arefully go through the code given below and answer the questions‬
C
‭based on it :‬
‭heStr‬‭
t =‬‭
" This is a test "‬
inputStr‬‭
‭ =‬‭input‬
(‭
‭"
‬ Enter integer: "‬
)‬

inputlnt‬‭
‭ =‬‭int‬
(inputStr)‬

testStr‬‭
‭ =‬‭theStr‬
while‬‭
‭ inputlnt‬‭ >=‬‭0‬‭
:‬
testStr‬‭
‭ =‬‭
testStr[‬ 1‭
‭:‬‭
-
‬1‬
]‬

inputlnt‬‭
‭ =‬‭
inputlnt‬‭ -‬‭
1‬
testBool‬‭
‭ =‬‭'t'‬‭
in‬‭testStr‬
print‬‭
‭ (theStr)‬ # Line
‭ 1‬
print‬‭
‭ (testStr)‬ # Line
‭ 2‬
print‬‭
‭ (inputlnt)‬ # Line
‭ 3‬
print‬‭
‭ (testBool)‬ # Line
‭ 4‬

‭(i) Given the input integer 3, what output is produced by Line 1?‬

‭ .‬ ‭This is a test‬
1
‭2.‬ ‭This is a‬
‭3.‬ ‭is a test‬
‭4.‬ ‭is a‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 1 — This is a test‬

‭(ii) Given the input integer 3, what output is produced by Line 2?‬

‭ .‬ ‭This is a test‬
1
‭2.‬ ‭s is a t‬
‭3.‬ ‭is a test‬
‭4.‬ ‭is a‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 2 — s is a t‬

‭Explanation‬

‭ s input is 3 and inside the while loop, inputlnt decreases by 1 in each‬


A
‭iteration so the while loop executes 4 times for inputlnt values 3, 2, 1, 0.‬

‭ st Iteration‬
1
‭testStr = "This is a test"‬

‭ nd Iteration‬
2
‭testStr = "his is a tes"‬

‭ rd Iteration‬
3
‭testStr = "is is a te"‬

‭ th Iteration‬
4
‭testStr = "s is a t"‬

‭(iii) Given the input integer 2, what output is produced by Line 3?‬

‭ .‬ ‭0‬
1
‭2.‬ ‭1‬
‭ .‬ ‭2‬
3
‭4.‬ ‭3‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 5 — None of these‬

‭Explanation‬

‭ alue of inputlnt will be -1 as till inputlnt >= 0 the while loop will continue‬
V
‭executing.‬

‭(iv) Given the input integer 2, what output is produced by Line 4?‬

‭ .‬ ‭False‬
1
‭2.‬ ‭True‬
‭3.‬ ‭0‬
‭4.‬ ‭1‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 2 — True‬

‭Explanation‬

‭ s input is 2 and inside the while loop, inputlnt decreases by 1 in each‬


A
‭iteration so the while loop executes 3 times for inputlnt values 2, 1, 0.‬

‭ st Iteration‬
1
‭testStr = "This is a test"‬

‭ nd Iteration‬
2
‭testStr = "his is a tes"‬

‭ rd Iteration‬
3
‭testStr = "is is a te"‬
‭ fter the while loop finishes executing, value of testStr is "is is a te". 't' in‬
A
‭testStr returns True as letter t is present in testStr.‬

‭Question 4‬
‭ arefully go through the code given below and answer the questions‬
C
‭based on it :‬
‭estStr‬‭
t =‬‭"abcdefghi"‬
inputStr‬‭
‭ =‬‭input‬‭(‬
"Enter integer:"‬
‭ )‬

inputlnt‬‭
‭ =‬‭int‬(inputStr)‬

count‬‭
‭ =‬‭2‬
newStr‬‭
‭ =‬‭
''‬
while‬‭
‭ count‬‭ <=‬‭inputlnt :‬
newStr‬‭
‭ =‬‭
newStr‬‭+‬‭testStr[‬
0‬‭
‭ : count]‬
testStr‬‭
‭ =‬‭testStr[‬2‭
‭:‬]‬ #Line 1‬

count‬‭
‭ =‬‭
count‬‭+‬‭
1‬
print‬‭
‭ (newStr)‬ # Line 2‬

print‬‭
‭ (testStr)‬ # Line 3‬

print‬‭
‭ (count)‬ # Line 4‬

print‬‭
‭ (inputlnt)‬ # Line 5‬

‭(i) Given the input integer 4, what output is produced by Line 2?‬

‭ .‬ ‭abcdefg‬
1
‭2.‬ ‭aabbccddeeffgg‬
‭3.‬ ‭abcdeefgh‬
‭4.‬ ‭ghi‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 3 — abcdeefgh‬

‭Explanation‬

I‭nput integer is 4 so while loop will execute 3 times for values of count as‬
‭2, 3, 4.‬
‭1st Iteration‬
‭newStr = newStr + testStr[0:2]‬
‭⇒ newStr = '' + ab‬
‭⇒ newStr = ab‬

t‭estStr = testStr[2:]‬
‭⇒ testStr = cdefghi‬

‭2nd Iteration‬
‭newStr = newStr + testStr[0:3]‬
‭⇒ newStr = ab + cde‬
‭⇒ newStr = abcde‬

t‭estStr = testStr[2:]‬
‭⇒ testStr = efghi‬

‭3rd Iteration‬
‭newStr = newStr + testStr[0:4]‬
‭⇒ newStr = abcde + efgh‬
‭⇒ newStr = abcdeefgh‬

t‭estStr = testStr[2:]‬
‭⇒ testStr = ghi‬

‭(ii) Given the input integer 4, what output is produced by Line 3?‬

‭ .‬ ‭abcdefg‬
1
‭2.‬ ‭aabbccddeeffgg‬
‭3.‬ ‭abcdeefgh‬
‭4.‬ ‭ghi‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 4 — ghi‬

‭Explanation‬
I‭nput integer is 4 so while loop will execute 3 times for values of count as‬
‭2, 3, 4.‬

‭1st Iteration‬
‭testStr = testStr[2:]‬
‭⇒ testStr = cdefghi‬

‭2nd Iteration‬
‭testStr = testStr[2:]‬
‭⇒ testStr = efghi‬

‭3rd Iteration‬
‭testStr = testStr[2:]‬
‭⇒ testStr = ghi‬

‭(iii) Given the input integer 3, what output is produced by Line 4?‬

‭ .‬ ‭0‬
1
‭2.‬ ‭1‬
‭3.‬ ‭2‬
‭4.‬ ‭3‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 5 — None of these‬

‭Explanation‬

‭ ooking at the condition of while loop —‬‭while count‬‭<= inputlnt‬‭, the‬


L
‭while loop will stop executing when count becomes greater than inputlnt.‬
‭Value of inputlnt is 3 so when loop stops executing count will be 4.‬

‭(iv) Given the input integer 3, what output is produced by Line 5?‬

‭ .‬ ‭0‬
1
‭2.‬ ‭1‬
‭ .‬ ‭2‬
3
‭4.‬ ‭3‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 4 — 3‬

‭Explanation‬

‭ he input is converted from string to integer and after that its value is‬
T
‭unchanged in the code so line 5 prints the input integer 3.‬

‭(v) Which statement is equivalent to the statement found in Line 1?‬

‭ .‬ ‭testStr = testStr[2:0]‬
1
‭2.‬ ‭testStr = testStr[2:-1]‬
‭3.‬ ‭testStr = testStr[2:-2]‬
‭4.‬ ‭testStr = testStr - 2‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 5 — None of these‬

‭Question 5‬
‭ arefully go through the code given below and answer the questions‬
C
‭based on it :‬
‭nputStr‬‭
i =‬‭input‬(‭
‭"
‬ Give me a string:"‬)‬

biglnt‬‭
‭ =‬‭
0‬
littlelnt‬‭
‭ =‬‭
0‬
otherlnt‬‭
‭ =‬‭0‬
for‬‭
‭ ele‬‭
in‬‭inputStr:‬
if‬‭
‭ ele‬‭>=‬‭'a'‬‭and‬‭
ele‬‭
<=‬‭
'm'‬:‬
‭ # Line 1‬

littlelnt‬‭
‭ =‬‭
littlelnt‬‭+‬‭
1‬
elif‬‭
‭ ele‬‭>‬‭'m'‬‭and‬‭
ele‬‭
<=‬‭'z'‬
:‬

biglnt‬‭
‭ =‬‭
biglnt‬‭+‬‭
1‬
else‬
‭ :‬

otherlnt‬‭
‭ =‬‭
otherlnt‬‭
+‬‭
1‬
print‬‭
‭ (biglnt)‬ # Line
‭ 2‬
print‬‭
‭ (littlelnt)‬ # Line
‭ 3‬
print‬‭
‭ (otherlnt)‬ # Line
‭ 4‬
print‬‭
‭ (inputStr.‬
isdigit‬
‭ ())‬‭
‭ # Line 5‬

‭(i) Given the input abcd what output is produced by Line 2?‬

‭ .‬ ‭0‬
1
‭2.‬ ‭1‬
‭3.‬ ‭2‬
‭4.‬ ‭3‬
‭5.‬ ‭4‬

‭Answer‬

‭Option 1 — 0‬

‭Explanation‬

‭In the input abcd, all the letters are between a and m so the condition —‬
if ele >= 'a' and ele <= 'm'‬‭is always true. Hence,‬‭biglnt is 0.‬

‭(ii) Given the input Hi Mom what output is produced by Line 3?‬

‭ .‬ ‭0‬
1
‭2.‬ ‭1‬
‭3.‬ ‭2‬
‭4.‬ ‭3‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 3 — 2‬

‭Explanation‬
‭In the input Hi Mom, only two letters i and m satisfy the condition —‬‭if‬
ele >= 'a' and ele <= 'm'‬
‭ ‭. Hence, value of littlelnt‬‭is 2.‬

‭(iii) Given the input Hi Mom what output is produced by Line 4?‬

‭ .‬ ‭0‬
1
‭2.‬ ‭1‬
‭3.‬ ‭2‬
‭4.‬ ‭3‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 4 — 3‬

‭Explanation‬

I‭n the input Hi Mom, 3 characters H, M and space are not between a and‬
‭z. So for these 3 characters the statement in else part —‬‭otherlnt =‬
otherlnt + 1‬‭is executed. Hence, value of otherlnt‬‭is 3.‬

‭(iv) Given the input 1+2 =3 what output is produced by Line 5?‬

‭ .‬ ‭0‬
1
‭2.‬ ‭1‬
‭3.‬ ‭True‬
‭4.‬ ‭False‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 4 — False‬

‭Explanation‬

‭ s all characters in the input string 1+2 =3 are not digits hence isdigit()‬
A
‭returns False.‬
(‭ v) Give the input Hi Mom, what changes result from modifying Line 1‬
‭from‬

i‭f ele >= 'a' and ele <='m' to the expression‬


‭if ele >= 'a' and ele < 'm'?‬

‭ .‬ ‭No change‬
1
‭2.‬ ‭otherlnt would be larger‬
‭3.‬ ‭littlelnt would be larger‬
‭4.‬ ‭biglnt would be larger‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 2 — otherlnt would be larger‬

‭Explanation‬

‭ or letter m, now else case will be executed increasing the value of‬
F
‭otherlnt.‬

‭Question 6‬
‭ arefully go through the code given below and answer the questions‬
C
‭based on it :‬
‭n1Str‬‭
i =‬‭
input‬
(‭
‭ "
‬ Enter string of digits: "‬
)‬

in2Str‬‭
‭ =‬‭
input‬
(‭
‭ "
‬ Enter string of digits: "‬
)‬

if‬‭
‭ len‬
(in1Str)‬
‭ >‭
‭l‬en‬
(in2Str):‬

small‬‭
‭ =‬‭
in2Str‬
large‬‭
‭ =‬‭
in1Str‬
else‬
‭ :‬

small‬‭
‭ =‬‭
in1Str‬
large‬‭
‭ =‬‭
in2Str‬
newStr‬‭
‭ =‬‭
''‬
for‬‭
‭ element‬‭ in‬‭small:‬
result‬‭
‭ =‬‭
int‬ (element)‬‭
‭ +‬‭
int‬
(large[‬
‭ 0‭
‭]‬)‬
newStr‬‭
‭ =‬‭
newStr‬‭ +‬‭
str‬
(result)‬

‭arge‬‭
l =‬‭
large[‬
1‭
‭ :
‬]‬
print‬‭
‭ (‬len‬
‭ (newStr))‬
‭ ‭
# Line 1‬
print‬‭
‭ (newStr)‬ #
‭ Line 2‬
print‬‭
‭ (large)‬ #
‭ Line 3‬
print‬‭
‭ (small)‬ #
‭ Line 4‬

(‭ i) Given a first input of 12345 and a second input of 246, what result is‬
‭produced by Line 1?‬

‭ .‬ ‭1‬
1
‭2.‬ ‭3‬
‭3.‬ ‭5‬
‭4.‬ ‭0‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 2 — 3‬

‭Explanation‬

‭ s length of smaller input is 3, for loop executes 3 times so 3 characters‬


A
‭are added to newStr. Hence, length of newStr is 3.‬

(‭ ii) Given a first input of 12345 and a second input of 246, what result is‬
‭produced by Line 2?‬

‭ .‬ ‭369‬
1
‭2.‬ ‭246‬
‭3.‬ ‭234‬
‭4.‬ ‭345‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 1 — 369‬

‭Explanation‬
‭For loop executes 3 times as length of smaller input is 3.‬

‭1st Iteration‬
‭result = 2 + 1‬
‭⇒ result = 3‬

‭ ewStr = '' + '3'‬


n
‭⇒ newStr = '3'‬

‭large = 2345‬

‭2nd Iteration‬
‭result = 4 + 2‬
‭⇒ result = 6‬

‭ ewStr = '3' + '6'‬


n
‭⇒ newStr = '36'‬

‭large = 345‬

‭3rd Iteration‬
‭result = 6 + 3‬
‭⇒ result = 9‬

‭ ewStr = '36' + '9'‬


n
‭⇒ newStr = '369'‬

‭large = 45‬

‭Final value of newStr is '369'.‬

(‭ iii) Given a first input of 123 and a second input of 4567, what result is‬
‭produced by Line 3?‬

‭ .‬ ‭3‬
1
‭2.‬ ‭7‬
‭3.‬ ‭12‬
‭4.‬ ‭45‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 2 — 7‬

‭Explanation‬

‭ or loop executes 3 times as length of smaller input is 3. Initial value of‬


F
‭large is 4567.‬

‭1st Iteration‬
‭large = large[1:]‬
‭⇒ large = 567‬

‭ nd Iteration
2 large = large[1:]‬
‭⇒ large = 67‬

‭ rd Iteration
3 large = large[1:]‬
‭⇒ large = 7‬

(‭ iv) Given a first input of 123 and a second input of 4567, what result is‬
‭produced by Line 4?‬

‭ .‬ ‭123‬
1
‭2.‬ ‭4567‬
‭3.‬ ‭7‬
‭4.‬ ‭3‬
‭5.‬ ‭None of these‬

‭Answer‬

‭Option 1 — 123‬

‭Explanation‬

‭ s length of 123 is less than length of 4567 so 123 is assigned to‬


A
‭variable small and gets printed in line 4.‬
‭Question 7a‬
‭Find the output if the input string is 'Test'.‬
‭‬‭
S =‬‭input‬ (‬
‭ "Enter String :"‬
‭ )‬

RS‬‭
‭ =‬‭" "‬
for‬‭
‭ ch‬‭ in‬‭S‬‭
:‬
RS‬‭
‭ =‬‭
ch‬‭+‬‭RS‬
print‬
‭ (‭
‭S‬‬‭
+‬‭RS‬
)‬

‭Answer‬

‭Output‬

TesttseT‬

‭Explanation‬

‭ he for loop reverses the input string and stores the reversed string in‬
T
‭variable RS. After that original string and reversed string are‬
‭concatenated and printed.‬

‭Question 7b‬
‭Find the output if the input string is 'Test'.‬
‭‬‭
S =‬‭input‬ (‬
‭ "Enter String :"‬
‭ )‬

RS‬‭
‭ =‬‭" "‬
for‬‭
‭ ch‬‭ in‬‭S‬‭
:‬
RS‬‭
‭ =‬‭
ch‬‭+‬‭2‬‭
+‬‭
RS‬
print‬
‭ (‭
‭S‬‬‭
+‬‭RS‬
)‬

‭Answer‬

‭ he program gives an error at line‬‭RS = ch + 2 + RS‬‭.‬‭The operands to +‬


T
‭are a mix of string and integer which is not allowed in Python.‬

‭Question 8a‬
‭Find the errors. Find the line numbers causing errors.‬

‭ .‬ ‭S = "PURA VIDA"‬
1
‭2.‬ ‭print(S[9] + S[9 : 15])‬

‭Answer‬

‭ he error is in line 2. Length of string S is 9 so its indexes range for 0 to‬


T
‭8. S[9] is causing error as we are trying to access out of bound index.‬

‭Question 8b‬
‭Find the errors. Find the line numbers causing errors.‬

‭ .‬ ‭S = "PURA VIDA"‬
1
‭2.‬ ‭S1 = S[: 10] +S[10 :]‬
‭3.‬ ‭S2 = S[10] + S[-10]‬

‭Answer‬

‭ he error is in line 3. Length of string S is 9 so its forward indexes range‬


T
‭for 0 to 8 and backwards indexes range from -1 to -9. S[10] and S[-10]‬
‭are trying to access out of bound indexes.‬

‭Question 8c‬
‭Find the errors. Find the line numbers causing errors.‬

‭ .‬ ‭S = "PURA VIDA"‬
1
‭2.‬ ‭S1 = S * 2‬
‭3.‬ ‭S2 = S1[-19] + S1[-20]‬
‭4.‬ ‭S3 = S1[-19 :]‬

‭Answer‬

‭ he error is in line 3. S1[-19] and S1[-20] are trying to access out of‬
T
‭bound indexes.‬
‭Question 8d‬
‭Find the errors. Find the line numbers causing errors.‬

‭ .‬ ‭S = "PURA VIDA"‬
1
‭2.‬ ‭S1 = S[: 5]‬
‭3.‬ ‭S2 = S[5 :]‬
‭4.‬ ‭S3 = S1 * S2‬
‭5.‬ ‭S4 = S2 + '3'‬
‭6.‬ ‭S5 = S1 + 3‬

‭Answer‬

‭ he errors are in line 4 and line 6. Two strings cannot be multiplied. A‬


T
‭string and an integer cannot be added.‬

‭Question 9‬
‭What is the output produced?‬

‭(i) >>> "whenever" .find("never")‬

‭(ii) >>> "whenever" .find("what")‬

‭Answer‬

(‭ i) 3‬
‭The starting index of substring "never" in "whenever" is 3.‬

(‭ ii) -1‬
‭Substring "what" is not present in "whenever".‬

‭Question 10‬
‭What is the output produced?‬

‭(i) >>> "-".join(['123','365','1319'])‬

‭(ii) >>> " ".join(['Python', 'is', 'fun'])‬


‭Answer‬

‭(i) '123-365-1319'‬

‭(ii) 'Python is fun'‬

‭Question 11‬
‭Given a string S, write expressions to print‬

‭ .‬ ‭first five characters of S‬


1
‭2.‬ ‭Ninth character of S‬
‭3.‬ ‭reversed S‬
‭4.‬ ‭alternate characters from reversed S‬

‭Answer‬

‭ .‬ ‭print(S[:5])‬
1
‭2.‬ ‭print(S[8])‬
‭3.‬ ‭for a in range(-1, (-len(S) - 1), -1) :‬
‭print(S[a], end = '')‬
‭4.‬ ‭for a in range(-1, (-len(S) - 1), -2) :‬
‭print(S[a], end = '')‬

‭ ype C: Programming Practice/Knowledge based‬


T
‭Questions‬

‭Question 1‬
‭ rite a program to count the number of times a character occurs in the‬
W
‭given string.‬

‭Solution‬

‭tr‬‭
s =‬‭
input‬(‭
‭"‬Enter the string: "‬
)‬

ch‬‭
‭ =‬‭
input‬
(‭
‭ "
‬Enter the character to count: "‬
);‬

c‬‭
‭ =‬‭str.‬
count‬
‭ (ch)‬

print‬
‭ (ch,‬‭
‭ "occurs"‬
, c,‬‭
‭ "times"‬
)‬

‭Output‬

‭nter the string: KnowledgeBoat‬


E
Enter the character to count: e‬

e occurs 2 times‬

‭Question 2‬
‭Write a program which replaces all vowels in the string with '*'.‬

‭Solution‬

‭tr‬‭
s =‬‭input‬(‭
‭"‬Enter the string: "‬
)‬

newStr‬‭
‭ =‬‭
""‬
for‬‭
‭ ch‬‭in‬‭str :‬
lch‬‭
‭ =‬‭ch.‬lower‬
‭ ()‬

if‬‭
‭ lch‬‭==‬‭ 'a'‬‭\‬
or‬‭
‭ lch‬‭ ==‬‭
'e'‬‭\‬
or‬‭
‭ lch‬‭ ==‬‭
'i'‬‭\‬
or‬‭
‭ lch‬‭ ==‬‭
'o'‬‭\‬
or‬‭
‭ lch‬‭ ==‬‭
'u'‬‭:‬
newStr‬‭
‭ +=‬‭'*'‬
else‬‭
‭ :‬
newStr‬‭
‭ +=‬‭ch‬
print‬
‭ (newStr)‬

‭Output‬

‭nter the string: Computer Studies‬


E
C*mp*t*r St*d**s‬

‭Question 3‬
‭ rite a program which reverses a string and stores the reversed string in‬
W
‭a new string.‬
‭Solution‬

‭tr‬‭
s =‬‭input‬(‭
‭"‬Enter the string: "‬
)‬

newStr‬‭
‭ =‬‭
""‬
for‬‭
‭ ch‬‭in‬‭str :‬
newStr‬‭
‭ =‬‭ch‬‭
+‬‭
newStr‬
print‬
‭ (newStr)‬

‭Output‬

‭nter the string: computer studies‬


E
seiduts retupmoc‬

‭Question 4‬
‭ rite a program that prompts for a phone number of 10 digits and two‬
W
‭dashes, with dashes after the area code and the next three numbers.‬
‭For example, 017-555-1212 is a legal input. Display if the phone number‬
‭entered is valid format or not and display if the phone number is valid or‬
‭not (i.e., contains just the digits and dash at specific places.)‬

‭Solution‬

‭hNo‬‭
p =‬‭
input‬(‭
‭"‬Enter the phone number: "‬
)‬

length‬‭
‭ =‬‭
len‬(phNo)‬

if‬‭
‭ length‬‭ ==‬‭
12‬‭ \‬
and‬‭
‭ phNo[‬3‬
‭ ]‬‭
‭ ==‬‭"-"‬‭
\‬
and‬‭
‭ phNo[‬7‬
‭ ]‬‭
‭ ==‬‭"-"‬‭
\‬
and‬‭
‭ phNo[:‬ 3‭
‭]‬.‬
isdigit‬
‭ () \‬

and‬‭
‭ phNo[‬4‬
‭ :‭
‭7‬‭
]
‬.‬isdigit‬
‭ () \‬

and‬‭
‭ phNo[‬8‬
‭ :].‬
‭ isdigit‬
‭ () :‬

print‬
‭ (‬
‭ "Valid Phone Number"‬
‭ )‬

else‬‭
‭ :‬
print‬
‭ (‬
‭ "Invalid Phone Number"‬
‭ )‬

‭Output‬

‭nter the phone number: 017-555-1212‬


E
Valid Phone Number‬

=====================================‬

‭nter the phone number: 017-5A5-1212‬


E
Invalid Phone Number‬

‭Question 5‬
‭Write a program that should do the following :‬

‭‬p
● ‭ rompt the user for a string‬
‭●‬ ‭extract all the digits from the string‬
‭●‬ ‭If there are digits:‬
‭○‬ ‭sum the collected digits together‬
‭○‬ ‭print out the original string, the digits, the sum of the digits‬
‭●‬ ‭If there are no digits:‬
‭○‬ ‭print the original string and a message "has no digits"‬

‭Sample‬

‭●‬ g ‭ iven the input : abc123‬


‭prints abc123 has the digits 123 which sum to 6‬
‭●‬ ‭given the input : abcd‬
‭prints abcd has no digits‬

‭Solution‬

‭tr‬‭
s =‬‭input‬(‭
‭"‬Enter the string: "‬
)‬

sum‬‭
‭ =‬‭0‬
digitStr‬‭
‭ =‬‭''‬
for‬‭
‭ ch‬‭in‬‭str :‬
if‬‭
‭ ch.‬isdigit‬
‭ () :‬

digitStr‬‭
‭ +=‬‭ch‬
sum‬‭
‭ +=‬‭int‬(ch)‬

if‬‭
‭ not‬‭digitStr :‬
print‬
‭ (str,‬‭
‭ "has no digits"‬
)‬

else‬‭
‭ :‬
print‬
‭ (str,‬‭
‭ "has the digits"‬
, digitStr,‬‭
‭ "which‬‭
sum to"‬
, sum)‬

‭Output‬

‭nter the string: abc123‬


E
abc123 has the digits 123 which sum to 6‬

=====================================‬

‭nter the string: KnowledgeBoat‬


E
KnowledgeBoat has no digits‬

‭Question 6‬
‭ rite a program that should prompt the user to type some sentence(s)‬
W
‭followed by "enter". It should then print the original sentence(s) and the‬
‭following statistics relating to the sentence(s) :‬

‭‬N
● ‭ umber of words‬
‭●‬ ‭Number of characters (including white-space and punctuation)‬
‭●‬ ‭Percentage of characters that are alphanumeric‬

‭Hints‬

‭●‬ A
‭ ssume any consecutive sequence of non-blank characters is a‬
‭word.‬

‭Solution‬

‭tr‬‭
s =‬‭
input‬(‭
‭"‬Enter a few sentences: "‬
)‬

length‬‭
‭ =‬‭
len‬
(str)‬

spaceCount‬‭
‭ =‬‭0‬
alnumCount‬‭
‭ =‬‭0‬

for‬‭
‭ ch‬‭
in‬‭str :‬
if‬‭
‭ ch.‬isspace‬
‭ () :‬

spaceCount‬‭
‭ +=‬‭1‬
elif‬‭
‭ ch.‬isalnum‬
‭ () :‬

alnumCount‬‭
‭ +=‬‭1‬

alnumPercent‬‭
‭ =‬‭
alnumCount‬‭
/‬‭
length‬‭
*‬‭
100‬
‭rint‬
p (‭
‭"‬Original Sentences:"‬
)‬

print‬
‭ (str)‬

‭rint‬
p (‭
‭"‬Number of words ="‬
, (spaceCount‬‭
‭ +‬‭
1‬))‬

print‬
‭ (‭
‭"‬Number of characters ="‬
, (length‬‭
‭ +‬‭1‭
)‬)‬
print‬
‭ (‭
‭"‬Alphanumeric Percentage ="‬
, alnumPercent)‬

‭Output‬

‭nter a few sentences: Python was conceived in the late 1980s by‬
E
Guido van Rossum at Centrum Wiskunde & Informatica (CWI) in the‬

Netherlands. Its implementation began in December 1989. Python‬

3.0 was released on 3 December 2008.‬

Original Sentences:‬

Python was conceived in the late 1980s by Guido van Rossum at‬

Centrum Wiskunde & Informatica (CWI) in the Netherlands. Its‬

implementation began in December 1989. Python 3.0 was released‬

on 3 December 2008.‬

Number of words = 34‬

Number of characters = 206‬

Alphanumeric Percentage = 80.48780487804879‬

‭Question 7‬
‭Write a Python program as per specifications given below:‬

‭‬R
● ‭ epeatedly prompt for a sentence (string) or for 'q' to quit.‬
‭●‬ ‭Upon input of a sentence s, print the string produced from s by‬
‭converting each lower case letter to upper case and each upper‬
‭case letter to lower case.‬
‭●‬ ‭All other characters are left unchanged.‬

‭ or example,‬
F
‭Please enter a sentence, or 'q' to quit : This is the Bomb!‬
‭tHIS IS THE bOMB!‬
‭Please enter a sentence, or 'q ' to quit : What's up Doc ???‬
‭ HAT'S UP dOC ???‬
w
‭Please enter a sentence, or 'q' to quit : q‬

‭Solution‬

while‬‭
‭ True‬‭ :‬
str‬‭
‭ =‬‭input‬(‭
‭"‬Please enter a sentence, or 'q' to‬‭
quit : "‬
)‬

newStr‬‭
‭ =‬‭
""‬
if‬‭
‭ str.‬lower‬
‭ ()‬‭
‭ ==‬‭"q"‬‭:‬
break‬

for‬‭
‭ ch‬‭in‬‭str :‬
if‬‭
‭ ch.‬islower‬
‭ () :‬

newStr‬‭
‭ +=‬‭ch.‬
upper‬
‭ ()‬

elif‬‭
‭ ch.‬isupper‬
‭ () :‬

newStr‬‭
‭ +=‬‭ch.‬
lower‬
‭ ()‬

else‬‭
‭ :‬
newStr‬‭
‭ +=‬‭ch‬
print‬
‭ (newStr)‬

‭Output‬

‭lease enter a sentence, or 'q' to quit : This is the Bomb!‬


P
tHIS IS THE bOMB!‬

Please enter a sentence, or 'q' to quit : What's up Doc ???‬

wHAT'S UP dOC ???‬

Please enter a sentence, or 'q' to quit : q‬

‭Question 8‬
‭Write a program that does the following :‬

‭ ‬ t‭akes two inputs : the first, an integer and the second, a string‬

‭●‬ ‭from the input string extract all the digits, in the order they‬
‭occurred, from the string.‬
‭○‬ ‭if no digits occur, set the extracted digits to 0‬
‭●‬ ‭add the integer input and the digits extracted from the string‬
‭together as integers‬
‭●‬ p
‭ rint a string of the form :‬
‭"integer_input + string_digits = sum"‬

‭ or example :‬
F
‭For inputs 12, 'abc123' → '12 + 123 = 135'‬
‭For inputs 20, 'a5b6c7' → '20 + 567 =587'‬
‭For inputs 100, 'hi mom' → '100 + 0 = 100'‬

‭Solution‬

‭um‬‭
n =‬‭
int‬
(‬
‭ input‬
‭ (‭
‭"‬Enter an integer: "‬
))‬

str‬‭
‭ =‬‭
input‬(‭
‭"‬Enter the string: "‬)‬

‭igitsStr‬‭
d =‬‭
''‬
digitsNum‬‭
‭ =‬‭
0‭
;‬‬

for‬‭
‭ ch‬‭
in‬‭
str :‬
if‬‭
‭ ch.‬
isdigit‬
‭ () :‬

digitsStr‬‭
‭ +=‬‭
ch‬

if‬‭
‭ digitsStr :‬
digitsNum‬‭
‭ =‬‭
int‬
(digitsStr)‬

print‬
‭ (num,‬‭
‭ "+"‬
, digitsNum,‬‭
‭ "="‬
, (num‬‭
‭ +‬‭
digitsNum))‬

‭Output‬

‭nter an integer: 12‬


E
Enter the string: abc123‬

12 + 123 = 135‬

=====================================‬

‭nter an integer: 20‬


E
Enter the string: a5b6c7‬

20 + 567 = 587‬

=====================================‬

Enter an integer: 100‬



‭nter the string: hi mom‬
E
100 + 0 = 100‬

‭Question 9‬
‭ rite a program that takes two strings from the user and displays the‬
W
‭smaller string in single line and the larger string as per this format :‬
1st letter
‭ last letter‬
2nd letter
‭ 2nd last letter‬
3rd letter
‭ 3rd last letter‬

‭ or example,‬
F
‭if the two strings entered are Python and PANDA then the output of the‬
‭program should be :‬
‭ANDA‬
P
P
‭ n‬
y
‭ o‬
t h‬

‭Solution‬

‭tr1‬‭
s =‬‭
input‬
(‭
‭"‬Enter first string: "‬
)‬

str2‬‭
‭ =‬‭
input‬
(‭
‭"‬Enter second string: "‬)‬

‭mall‬‭
s =‬‭
str1‬
large‬‭
‭ =‬‭
str2‬

if‬‭
‭ len‬
(str1)‬‭
‭ >‬‭
len‬
(str2) :‬

large‬‭
‭ =‬‭
str1‬
small‬‭
‭ =‬‭
str2‬

print‬
‭ (small)‬

‭enLarge‬‭
l =‬‭
len‬
(large)‬

for‬‭
‭ i‬‭
in‬‭
range‬
(lenLarge‬‭
‭ //‬‭
2‭
)‬ :‬
print‬
‭ (‬
‭ ' '‬‭
‭ *‬‭
i, large[i],‬‭
' '‬‭
*‬‭
(lenLarge‬‭
-‬‭
2‬‭
*‬‭
i),‬
large[lenLarge‬‭
‭ -‬‭
i‬‭
-‬‭
1‬], sep‬
‭ =‭
‭'‬'‬
)‬

‭Output‬

‭nter first string: Python‬


E
Enter second string: PANDA‬

PANDA‬

P
‭ n‬
y
‭ o‬
t h‬

‭Question 10‬
‭ rite a program to convert a given number into equivalent Roman‬
W
‭number (store its value as a string). You can use following guidelines to‬
‭develop solution for it:‬

‭●‬ F ‭ rom the given number, pick successive digits, using %10 and /10‬
‭to gather the digits from right to left.‬
‭●‬ ‭The rules for Roman Numerals involve using four pairs of symbols‬
‭for ones and five, tens and fifties, hundreds and five hundreds. An‬
‭additional symbol for thousands covers all the relevant bases.‬
‭●‬ ‭When a number is followed by the same or smaller number, it‬
‭means addition. "II" is two 1's = 2. "VI" is 5 + 1 = 6.‬
‭●‬ ‭When one number is followed by a larger number, it means‬
‭subtraction. "IX" is 1 before 10 = 9. "IIX isn't allowed, this would be‬
‭"VIII". For numbers from 1 to 9, the symbols are "I" and "V", and‬
‭the coding works like this. "I" , "II", "III", "IV", "V", "VI", "VII", "VIII",‬
‭"IX".‬
‭●‬ ‭The same rules work for numbers from 10 to 90, using "X" and "L".‬
‭For numbers from 100 to 900, using the symbols "C" and "D". For‬
‭numbers between 1000 and 4000, using "M".‬
‭ ere are some examples. 1994 = MCMXCIV, 1956 = MCMLVI, 3888=‬
H
‭MMMDCCCLXXXVIII‬

‭Solution‬

‭‬‭
n =‬‭
int‬(‭
‭i‬nput‬(‬
‭ "Enter the number: "‬
‭ ))‬

num‬‭
‭ =‬‭(‬
1000‬
‭ ,‬‭
‭ 900‬,‬ ‭
‭ 500‬
,‬‭
‭ 400‬
,‬‭
‭ 100‬,‬ ‭
‭ 90‬
,‬‭
‭ 50‬
,‬ ‭
‭ 40‬
,‬‭
‭ 10‬
,‬ ‭
‭ 9‬,‬
‭ 5‭
‭,‬‬ ‭
4‭
,‬‬
1‬
‭ )‬

rom‬‭
‭ =‬‭(‬
'M'‬
‭ ,‬ ‭
‭ 'CM'‬,‬‭
‭ 'D'‬
,‬‭
‭ 'CD'‬,‭
‭'‬C'‬
,‬

'XC'‬
‭ ,‬
‭ 'L'‬
‭ ,‭
‭ '
‬XL'‬,‬
‭ 'X'‬
‭ ,‬
‭ 'IX'‬
‭ ,‭
‭'‬V'‬
,‭
‭'‬IV'‬,‭
‭'‬I'‬
)‬

result‬‭
‭ =‬‭
''‬

for‬‭
‭ i‬‭
in‬‭
range‬(‭
‭l‬en‬(num)) :‬

count‬‭
‭ =‬‭
int‬
(n‬‭
‭ /‬‭
num[i])‬
result‬‭
‭ +=‬‭
str‬(rom[i]‬‭
‭ *‬‭
count)‬
n‬‭
‭ -=‬‭
num[i]‬‭ *‬‭count‬

print‬
‭ (result)‬

‭Output‬

‭nter the number: 1994‬


E
MCMXCIV‬

=====================================‬

‭nter the number: 1956‬


E
MCMLVI‬

=====================================‬

‭nter the number: 3888‬


E
MMMDCCCLXXXVIII‬

‭Question 11‬
‭ rite a program that asks the user for a string (only single space‬
W
‭between words) and returns an estimate of how many words are in the‬
‭string. (Hint. Count number of spaces)‬

‭Solution‬

‭tr‬‭
s =‬‭input‬ (‭
‭"‬Enter a string: "‬
)‬

count‬‭
‭ =‬‭0‬
for‬‭
‭ ch‬‭ in‬‭str :‬
if‬‭
‭ ch.‬isspace‬
‭ () :‬

count‬‭
‭ +=‬‭
1‬
print‬
‭ (‭
‭"‬No of words ="‬ , (count‬‭
‭ +‬‭
1‭
)‬)‬

‭Output‬

‭nter a string: Python was conceived in the late 1980s by Guido‬


E
van Rossum at Centrum Wiskunde & Informatica (CWI) in the‬

Netherlands.‬

No of words = 20‬

‭Question 12‬
‭ rite a program to input a formula with some brackets and checks, and‬
W
‭prints out if the formula has the same number of opening and closing‬
‭parentheses.‬

‭Solution‬

‭tr‬‭
s =‬‭
input‬
(‭
‭"‬Enter a formula: "‬
)‬

count‬‭
‭ =‬‭
0‬

for‬‭
‭ ch‬‭
in‬‭str :‬
if‬‭
‭ ch‬‭==‬‭
'('‬‭:‬
count‬‭
‭ +=‬‭
1‬
elif‬‭
‭ ch‬‭==‬‭')'‬‭:‬
count‬‭
‭ -=‬‭
1‬

if‬‭
‭ count‬‭
==‬‭
0‬‭
:‬
‭rint‬
p (‬
‭ "Formula has same number of opening and closing‬

parentheses"‬
‭ )‬

else‬‭
‭ :‬
print‬
‭ (‬
‭ "Formula has unequal number of opening and‬‭
‭ closing‬
parentheses"‬
‭ )‬

‭Output‬

‭nter a formula: s(s-a)(s-b)(s-c)‬


E
Formula has same number of opening and closing parentheses‬

=====================================‬

‭nter a formula: s((s-a)(s-b)(s-c)‬


E
Formula has unequal number of opening and closing parentheses‬

‭Question 13‬
‭ rite a program that inputs a line of text and prints out the count of‬
W
‭vowels in it.‬

‭Solution‬

‭tr‬‭
s =‬‭
input‬
(‭
‭"‬Enter a string: "‬
)‬

count‬‭
‭ =‬‭
0‬

for‬‭
‭ ch‬‭
in‬‭
str :‬
lch‬‭
‭ =‬‭
ch.‬
lower‬
‭ ()‬

if‬‭
‭ lch‬‭
==‬‭'a'‬‭\‬
or‬‭
‭ lch‬‭==‬‭
'e'‬‭\‬
or‬‭
‭ lch‬‭==‬‭
'i'‬‭\‬
or‬‭
‭ lch‬‭==‬‭
'o'‬‭\‬
or‬‭
‭ lch‬‭==‬‭
'u'‬‭:‬
count‬‭
‭ +=‬‭1‬

print‬
‭ (‭
‭"‬Vowel Count ="‬
, count)‬

‭Output‬
‭nter a string: Internet of Things‬
E
Vowel Count = 5‬

‭Question 14‬
‭ rite a program to input a line of text and print the biggest word (length‬
W
‭wise) from it.‬

‭Solution‬

‭tr‬‭
s =‬‭
input‬
(‭
‭"‬Enter a string: "‬
)‬

words‬‭
‭ =‬‭
str.‬
split‬
‭ ()‬

longWord‬‭
‭ =‬‭
''‬

for‬‭
‭ w‬‭
in‬‭words :‬
if‬‭
‭ len‬
(w)‬‭
‭ >‬‭
len‬
(longWord) :‬

longWord‬‭
‭ =‬‭
w‬

print‬
‭ (‭
‭"‬Longest Word ="‬
, longWord)‬

‭Output‬

‭nter a string: TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN‬


E
BAGAN‬

Longest Word = FOOTBALL‬

‭Question 15‬
‭ rite a program to input a line of text and create a new line of text where‬
W
‭each word of input line is reversed.‬

‭Solution‬

‭tr‬‭
s =‬‭
input‬(‭
‭ "
‬Enter a string: "‬
)‬

words‬‭
‭ =‬‭str.‬split‬
‭ ()‬

newStr‬‭
‭ =‬‭
""‬

for‬‭
‭ w‬‭
in‬‭
words :‬
‭w‬‭
r =‬‭
""‬
for‬‭
‭ ch‬‭
in‬‭w :‬
rw‬‭
‭ =‬‭
ch‬‭+‬‭rw‬
newStr‬‭
‭ +=‬‭rw‬‭
+‬‭" "‬

print‬
‭ (newStr)‬

‭Output‬

‭nter a string: Python is Fun‬


E
nohtyP si nuF‬

You might also like