Sample Papers Questions Based On Strings
Sample Papers Questions Based On Strings
questions based
on
Strings
Q-How to Reverse a String in
Python using slicing?
txt = "Salwan Public School"[::-1]
print(txt)
Q-From the string S = “CARPE DIEM”. Which
ranges return “DIE” and “CAR”?
S[6:9] for ―”DIE” and S[0:3] for ―”CAR”
Q-which of the following is not a Python legal string
operation?
(a)‟abc‟+‟abc‟
(b) „abc‟*3
(c)‟abc‟ + 3
(d)‟abc‟.lower()
Write a python script that traverses through an
input string and prints its characters in different
lines – two characters per line.
Find the OUTPUT:
def my_function(x):
return x[::-1]
print(mytxt)
Find the Output
word = 'banana'
if word[0] in "aeiou":
word = word + 'yay'
else:
word = word[1:] + word[0] + 'ay'
print(word)
Q-Which of the following are not valid strings in
Python?
(a)”Hello”
(b) ‘Hello‘
(c)”Hello‘
(d) ‘Hello”
(e) {Hello}
Q- '80000'.isnumeric()
Q-'1.0'.isnumeric()
Q- '-'.join(['a’,'b’,'c'])
f-strings make string interpolation really easy.
Using f-strings is similar to using format().
Q-'Ten10'.isalnum()
'Ten10.'.isalnum()
How to remove vowels from the
string
string = 'Hello 1 World 2’
vowels = ('a‘,'e‘,'i‘,'o‘,'u')
‘ ‘.join([c for c in string if c not in vowels])
x = txt.title()
print(x)
String split() Method
x = txt.split()
print(x)
['welcome', 'to', 'the', 'jungle']
What would following expression return?
(a)"Hello World".upper().lower()
(b) "Hello World".lower().upper()
(c) "Hello World".find( "Wor", 1, 6)
(d) "Hello World".find( "Wor")
(e) "Hello World".find( “wor”)
(f) "Hello World".isalpha()
(g) "HelloWorld12".isalnum()
(h) "1234".isdigit()
(i) "123FGH".isdigit()
(A)= 'hello world'
(D) = 6
(E) = -1
(F) = False
(g) =True
(h) = True
(I) = False
String replace() Method
x = txt.replace("bananas", "apples")
print(x)
Replace all occurrence of the word
"one":
x = txt.replace("one", "three")
print(x)
string.replace(oldvalue, newvalue,
count)
Replace the two first occurrence of the word
"one":
x = txt.replace("one", "three", 2)
print(x)