Computer Science : Core Python : Strings and String Manipulation
Evaluation
Part – I
Choose the best answer (1 Mark)
1.Which of the following is the output of the following python code? str1="TamilNadu" print(str1[::-1])
(a) Tamilnadu
(b) Tmlau
(c) udanlimaT
d) udaNlimaT
2.What will be the output of the following code? str1 = "Chennai Schools" str1[7] = "-"
(a) Chennai-Schools
(b) Chenna-School
(c) Type error
(D) Chennai
3.Which of the following operator is used for concatenation?
(a) +
(b) &
(c) *
d) =
4.Defining strings within triple quotes allows creating:
(a) Single line Strings
(b) Multiline Strings
(c) Double line Strings
(d) Multiple Strings
5.Strings in python:
(a) Changeable
(b) Mutable
(c) Immutable
(d) flexible
6.Which of the following is the slicing operator?
(a) { }
(b) [ ]
(c) < >
(d) ( )
7.What is stride?
(a) index value of slide operation
(b) first argument of slice operation
(c) second argument of slice operation
(d) third argument of slice operation
8.Which of the following formatting character is used to print exponential notation in upper case?
(a) %e
(b) %E
(c) %g
(d) %n
9.Which of the following is used as placeholders or replacement fields which get replaced along with format( ) function?
(a) { }
(b) < >
(c) ++
(d) ^^
10. The subscript of a string may be:
(a) Positive
(b) Negative
(c) Both (a) and (b)
(d) Either (a) or (b)
Part –II
Answer the following questions (2 Marks)
1. What is String?
Ans. (i) String is a data type
in python, which is used to handle array of characters,
(ii) String is a sequence of Unicode characters that may be a
combination of letters, numbers, or special symbols enclosed within single,
double or even triple quotes.
(iii) Example :
'Welcome to learning Python'
"Welcome to learning Python"
" "Welcome to learning Python" "
2. Do you modify a string in Python?
Ans. (i) Usually python does not support any modification in its
strings. But, it provides a function replace() to change all occurrences of a
particular character in a string.
(ii) General formate of replace
function:
replacef'charl",
"char2")
The replace function replaces all occurrences of charl with
char2.
(iii) Example :
>>> strl="How are you"
>>> print (strl)
How are you
>>> print (strl.replace("o", "e"))
Hew are yeu
3. How will you delete a string in Python?
Ans. Python will not allow deleting a particular character in a
string. Whereas you can remove entire string variable using del command.
>>> strl="How about you"
>>> print (strl)
How about you
>>> del strl
>>>> print (strl)
Traceback (most recent call last):
File "<pyshell#l 4>", line 1, in <module>
print (strl)
NameError: name 'strl' is not defined
4. What will be the output of the following python code?
str1 = “School”
print(str1*3)
Ans. Output: School School School
5. What is slicing?
Ans. (i) Slice is a substring of a main string. A substring can be
taken from the original string by using [] operator and index or subscript
values.
(ii) Thus, [] is also known as slicing operator. Using slice
operator, you have to slice one or more substrings from a main string.
Part –III
Answer the following questions (3 Marks)
1. Write a Python program to display the given pattern
C O M P U T E R
C O M P U T E
C O M P U T
C O M P U
C O M P
C O M
C O
C
Ans.
strl = input("Enter string")
str2 = ‘ ‘
index = len(strl)
for i in strl:
str2 = strl [0 : index]
index - = 1
print(str2)
2. Write a short about the followings with suitable example:
(a) capitalize( ) (b) swapcase( )
Ans.
3. What will be the output of the given python program?
str1 = "welcome"
str2 = "to school"
str3=str1[:2]+str2[len(str2)-2:]
print(str3)
Ans. weol
4. What is the use of format( )? Give an example.
Ans. (i) The format() function used with strings is very versatile
and powerful function used for formatting strings.
(ii) The curly braces {} are used as placeholders or replacement
fields which get replaced along with format() function.
(iii) numl=int
(input("Number 1:"))
num2=int (input('Number
2:"))
print ("The sum of {} and {} is {}".
format(numl,num2,(numl+num2)))
Output:
Number 1: 34
Number 2: 54
The sum of 34 and 54 is 88
5. Write a note about count( ) function in python.
Ans.
Part –IV
Answer the following questions (5 Marks)
1. Explain about string operators in python with suitable example.
Ans.
String Operators: Python provides the following operators for string operations.
These operators are useful to manipulate string.
(i) Concatenation (+): Joining of two or more strings is called as Concatenation. The
plus (+) operator is used to concatenate strings in python.
Example:
>>> "welcome" + "Python"
'welcomePython'
(ii) Append (+ =) : Adding more strings at the end of an existing string is known
as append. The operator += is used to append a new string with an existing
string.
Example :
>>> strl="Welcome to"
>>> strl+="Learn Python"
>>> print (strl)
Welcome to Learn Python
(iii) Repeating (*): The multiplication operator (*) is used to display a string in
multiple number of times.
Example :
>>> strl="Welcome"
>>> print (strl *4)
Welcome Welcome Welcome Welcome
HANDS ON EXPERIENCE
1. Write
a python program to find the length of a string.
Ans.
str = input ("Enter a string")
print (len(str))
2. Write a program to count the occurrences
of each word in a given string.
Ans.
Word = 1
str = input ("Enter a string")
For i in str:
If(i = = ‘’):
Word = word + 1
print("Number of words", Word)
3. Write a program to add a prefix text to
all the lines in a string.
Ans.
import textwrap
strl = input ("enter string")
t = textwrap. dedent (strl)
w = textwrap. fill (t, width = 50)
f = textwrap. indent (w, 1*1)
print ()
print (f)
print ()
4. Write a
program to print integers with **’ on the right of specified width.
Ans.
x = 4
y = 345
print ("width 2 "+" (*<3d)". format (x))
print ("width
6"+" (*<7d)". format (y))
5. Write a
program to create a mirror of the given string.
For example, "wel" =
"lew". •
Ans.
strl= input (“Enter a string”)
str2 = ‘ ‘
index = -1
for i in strl:
str2 = strl [index]
index - = 1
print (“The mirror image of the string is”, str2)
6. Write a
program to removes all the occurrences of a give character in a string.
Ans.
strl = input (“Enter string”)
Ch = input (“Enter a character to be removed”)
print (strl. Replace (ch, “ “ )
7. Write a
program to append a string to another string without using += operator.
Ans.
strl = input (“Enter first string”)
str2 - input (“Enter second string”)
print(strl + str2)
8. Write a
program to swap two strings.
Ans.
strl = input (“Enter string 1”)
Str2 = input (“Enter string 2”)
temp = strl
strl = str2
str2 = temp
print(“After swapping”)
print(strl. Str2)
9. Write a
program to replace a string with another string without using replace().
Ans.
strl = input (“Enter string”)
print (strl)
strl = input (“Enter string to the replaced”)
print (strl)
10. Write a
program to count the number of characters, words and lines in a given string.
Ans.
strl = input (“Enter string”)
c = 0
w = 1
fori in strl:
if i ! = “:
c = c + 1
else:
w = w + 1
x = strl. count (‘\n’) + 1
print (“Number of characters”, c - x + 1)
print (“Number of words”, w + x - 1)
print (“Number of lines”, x)
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.