0% found this document useful (0 votes)
93 views29 pages

Sample Papers Questions Based On Strings

The document provides examples of string operations and methods in Python like reversing a string using slicing, accessing substrings by index, checking if a string is a palindrome, and more. It also demonstrates common string methods like upper(), lower(), title(), split(), replace(), find(), isalpha(), isnumeric(), etc. and explains how to use them along with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views29 pages

Sample Papers Questions Based On Strings

The document provides examples of string operations and methods in Python like reversing a string using slicing, accessing substrings by index, checking if a string is a palindrome, and more. It also demonstrates common string methods like upper(), lower(), title(), split(), replace(), find(), isalpha(), isnumeric(), etc. and explains how to use them along with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Sample Papers

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]

mytxt = my_function("I wonder how this text looks


like backwards")

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- How many types of strings are supported by


Python?
WAP in python to check whether a
string is palindrome or not.
my_string=input("Enter string:")
if(my_string==my_string[::-1]):
   print("The string is a palindrome")
else:  
 print("The string isn't a palindrome")
Find the Output
difficulty = 'easy'
thing = 'exam‘

'That {} was {}!'.format(thing, difficulty)


 'That exam was easy!'
Find Output
Q-1name = 'Chris'
food = 'creme brulee‘
f'Hello. My name is {name} and I like {food}.‘

Q- '80000'.isnumeric()

Q-'1.0'.isnumeric()

Q-‘ ‘.join(reversed("hello world"))

Q- '-'.join(['a’,'b’,'c'])
f-strings make string interpolation really easy.
Using f-strings is similar to using format().

'Hello. My name is Chris and I like creme


brulee'
Q-animal = 'fish’
animal[0].upper() + animal[1:-1] + animal
[-1].upper()

Q-sentence = 'Sally sells sea shells by the sea


shore’
sentence.replace('sea', 'mountain')

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])

'Hll 1 Wrld 2'


 How is capitalize( ) function different from
upper( ) function ?
String title() Method

 Make the first letter in each word upper case:

 txt = "Welcome to my world"

x = txt.title()

print(x)
String split() Method

 Split a string into a list where each word is a


list item:

 txt = "welcome to the jungle"

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'

(B) =  'HELLO WORLD'


(c) =   'HELLO WORLD'


(D) = 6

(E) = -1

(F) = False

(g) =True

(h) = True

(I) = False
String replace() Method

 txt = "I like bananas"

x = txt.replace("bananas", "apples")

print(x)
Replace all occurrence of the word
"one":

 txt = "one one was a race horse, two two was


one too."

x = txt.replace("one", "three")

print(x)
string.replace(oldvalue, newvalue,
count)
 Replace the two first occurrence of the word
"one":

 txt = "one one was a race horse, two two was


one too."

x = txt.replace("one", "three", 2)

print(x)

You might also like