0% found this document useful (0 votes)
61 views

String Manipulation Type A Questions

Uploaded by

Himanshu Tonk
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)
61 views

String Manipulation Type A Questions

Uploaded by

Himanshu Tonk
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/ 7

String Manipulation

Type A : Short Answer Questions/Conceptual


Questions
Question 1: Write a Python script that traverses through an input
string and prints its characters in different lines — two characters
per line.
Answer:

Output

Question 2: Out of the following operators, which ones can be used


with strings in 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:
Strings are sequence of characters where each character has a
unique index. This implies that strings are iterable like lists but unlike
lists they are immutable so they cannot be modified at runtime.
Therefore, strings can't be considered as character lists. For example,
str = '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: Given a string S = "CARPE DIEM". If n = length/2
(length is the length of 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: From the string S = "CARPE DIEM", which ranges return
"DIE" and "CAR"?
Answer
S[6:9] returns DIE
S[:3] returns CAR
Question 8: What happens when from a string slice you skip the
start and/or end values of the slice?
Answer:
If start value is skipped, it is assumed as 0 i.e. the slice begins from
the start of the string.
If 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( )
"Hello World".lower( ).upper( )
"Hello World".find("Wor", 1, 6)
"Hello World".find("Wor")
"Hello World".find("wor")
"Hello World".isalpha( )
"Hello World".isalnum( )
"1234".isdigit( )
"123FGH".isdigit( )
Answer:
hello world
HELLO WORLD
-1
6
-1
False
False
True
False
Question 10: Which functions would you choose to use to remove
leading and trailing white spaces from a given string?
Answer
lstrip() removes leading white-spaces, rstrip() removes trailing white-
spaces and strip() removes leading and trailing white-spaces from a
given string.

Question 11
Try to find out if for any case, the string functions isalnum( ) and
isalpha( ) return the same result
Answer:
isalnum( ) and isalpha( ) return the same result in the following
cases:
If string contains only alphabets then both isalnum( ) and isalpha( )
return True. For example, "Hello".isalpha() and "Hello".isalnum()
return True.
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
To find for the occurrence a string within another string
To convert the first letter of a string to upper case
To capitalize all the letters of the string
To check whether all letters of the string are in capital letters
To remove from right of a string all string-combinations from a
given set of letters
To remove all white spaces from the beginning of a string
Answer
isdigit()
find()
capitalize()
upper()
isupper()
rstrip(characters)
lstrip()
Question 13: In a string slice, the start and end values can be
beyond limits. Why?
Answer:
String slicing always returns a subsequence and empty subsequence
is 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: Can you specify an out of bound index when accessing
a single character from a string? Why?
Answer:
We cannot specify an out of bound index when accessing a single
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 bounds there is no character to return from the given index
hence Python throws string index out of range error.
Question 15: Can you add two strings? What effect does ' + ' have
on strings?
Answer:
Yes two strings can be added using the '+' operator. '+' operator
concatenates two strings.

You might also like