Chapter – 5: STRING MANIPULATION
Very Short answer Type Questions
Q.1 which of the following is not a Python legal string operation?
(a)‟abc‟+‟abc‟ (b) „abc‟*3 (c)‟abc‟ + 3 (d)‟abc‟.lower()
Q.2 Out of the following operators, which ones can be used with strings?
=, -, *, /, //, %, >, <>, in, not in, <=
Q.3 From the string S = “CARPE DIEM”. Which ranges return “DIE” and “CAR”?
Q.4 Given a string S = “CARPE DIEM”. If n is length/2 then what would following return?
(a) S[:n] (b) S[n:] (c) S[n:n] (d) S[1:n] (e) S[n:length-1]
Q.5 What would following expression return?
(a) ”Hello World”.upper().lower() (b) ”Hello
(c) ”Hello World”.find(“Wor”,1,6) (d) ”Hello World”.find(“Wor”)
(e) ”Hello World”.find(“wor”) (f) ”Hello World”.isalpha()
(g) ”Hello World”.isalnum() (h) ”Hello World”.isdigit()
(i) “123FGH”.isdigit()
Short Answer Type Questions
Q.1 What is a string slice? How is it useful?
Q.2 Write a python script that traverses through an input string and prints its characters
in different lines – two characters per line.
Q.3 Which functions would you chose to use to remove leading and trailing white spaces
from a given string?
Q.4 Suggest appropriate functions for the following tasks –
(a) To check whether the string contains digits.
(b) To find the occurrence a string within another string.
(c) To convert the first letter of a string to upper case.
(d) To convert all the letters of a string to upper case.
(f) To check whether all the letters of the string are in capital letters.
(g) to remove all the white spaces from the beginning of a string.
Q.5 Find the errors -
s=”PURA
VIDA”
Print(s[9] +
s[9:15])
Q.6 Find the output – if we give input as “Hello”
Skill Based
Questions
Q.1 WAP to print following pattern without using any nested loop.
#
##
###
####
#####
Q.2 WAP to print the number of occurrences of a substring
into a line.
Q.3 WAP to check the given string is palindrome or not.
Q.4 WAP that:
o Prompt the user for a string
o Extract all the digits from the string.
o If there are digits
Sum the collected digits together.
Printout:
x The original string
x The digits
x The sum of the digits
o If there are no digits
Print the original string
A message “Has no Digits”