0% found this document useful (0 votes)
9 views5 pages

String Worksheet 2

The document contains a series of questions and answers related to string manipulation in Python, including string slicing, operators, and built-in functions. It explains how to traverse strings, the legality of certain operations, and the behavior of various string methods. Additionally, it discusses the immutability of strings and provides examples of using operators like '+' for concatenation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

String Worksheet 2

The document contains a series of questions and answers related to string manipulation in Python, including string slicing, operators, and built-in functions. It explains how to traverse strings, the legality of certain operations, and the behavior of various string methods. Additionally, it discusses the immutability of strings and provides examples of using operators like '+' for concatenation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

String Worksheet-2

Question 1 Write a Python script that traverses through an input string


and prints its characters in different lines — two characters per line.
Answer
str = input("Enter the string: ")
length = len(str)
for a in range(0, length, 2):
print(str[a:a+2])
Output
Enter the string: KnowledgeBoat
Kn
ow
le
dg
eB
oa
t

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) '[Link]()
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 is 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

1. S[6:9] returns DIE


2. 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?

1. "Hello World".upper( ).lower( )


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
2. HELLO WORLD
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
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:

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:

[Link] check whether the string contains digits


[Link] find for the occurrence a string within another string
[Link] convert the first letter of a string to upper case
[Link] capitalize all the letters of the string
[Link] check whether all letters of the string are in capital letters
[Link] 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

1. isdigit()
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
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