Ch10.
String Manipulation
1. Can you say strings are character lists? Why? Why not?
Ans: 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'
str[0] = 'b' # invalid statement as strings are immuatble
# Considering character lists
strList = ['c', 'a', 't']
strList[0] = 'b' #valid as Lists are mutable.
2. What happens when from a string slice you skip the start and/or end values of the
slice?
Ans: 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.
3. Which functions would you choose to use to remove leading and trailing white
spaces from a given string?
Ans: lstrip() removes leading white-spaces, rstrip() removes trailing white-spaces and
strip() removes leading and trailing white-spaces from a given string.
4. Can you specify an out of bound index when accessing a single character from a string? Why?
Ans: No, you cannot specify an out-of-bound index when accessing a single character from a string in
Python.
Reason:A string in Python is a sequence of characters, and every character has a fixed position (index).
If you try to access a position that does not exist, Python cannot find a character there and therefore raises
an error called:
IndexError: string index out of range
Example:
s = "hello"
print(s[10]) # trying to access index 10
Output:
IndexError: string index out of range
5. What is the result of the following expression?
text = "Test.\nNext line."
print (text)
Ans:
Test.
Next line.
[Link] is the result of the following expression?
Ans:
3 , 012 - 234
s = '0123456789' 012 - 3456789 , 3456789
print(s[3], ", ", s[0 : 3], " - ", s[2 : 5])
print(s[:3], " - ", s[3:], ", ", s[3:100])
print(s[20:], s[2:1], s[1:1])
7. Find the errors. Find the line numbers causing errors.
S = "PURA VIDA" Ans: The error is in line 3. S1[-19]
S1 = S * 2 and S1[-20] are trying to access out of
S2 = S1[-19] + S1[-20] bound indexes.
S3 = S1[-19 :]
[Link] is the output produced?
Answer
(i) >>> "-".join(['123','365','1319']) (i) '123-365-1319'
(ii) 'Python is fun'
(ii) >>> " ".join(['Python', 'is', 'fun'])
9. Given a string S, write expressions to print
i. first five characters of S Ans: i. S[0:5] or S[:5]
ii. Ninth character of S ii. S[8]
iii. reversed S
iii. S[::-1] iv. S[::-2]
iv. alternate characters from reversed S
[Link] a program that should prompt the user to type some sentence(s) followed by
"enter". It should then print the original sentence(s) and the following statistics
relating to the sentence(s) :
i. Number of words
ii. Number of characters (including white-space and punctuation)
iii. Percentage of characters that are alphanumeric
Hints: Assume any consecutive sequence of non-blank characters is a word.
s = input("Enter a sentence: ")
# Print the original sentence
print("Original sentence:", s)
# 1. Number of words
# Words are sequences of non-blank characters, so we split by spaces
words = [Link]()
num_words = len(words)
# 2. Number of characters (including spaces and punctuation)
num_chars = len(s)
# 3. Percentage of characters that are alphanumeric
alnum_count = 0
for ch in s:
if [Link](): # checks letters and digits
alnum_count += 1
if num_chars > 0:
percent_alnum = (alnum_count / num_chars) * 100
else:
percent_alnum = 0
# Print the statistics
print("Number of words:", num_words)
print("Number of characters:", num_chars)
print("Percentage of alphanumeric characters: “,percent_alnum)