wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python String Operations and Methods

Total questions: 100

Worksheet time: 50mins

Name
Class
Date
1.

What is the output of the following Python code?
s = "cbse@2024"
count = 0
for char in s:
----if char.isdigit():
--------count += 1
print(count)

a)

3

b)

4

c)

5

d)

9

2.

What is the output of the following Python code?
s = "Python"
result = ""
for i in range(len(s)):
------if i % 2 == 0:
---------result += s[i].upper()






-- - - else:
----------result += s[i].lower()

print(result)

a)

"pYtHoN"

b)

"PyThOn"

c)

"PYTHON"

d)

"pYtHon"

3.

What is the output of the following Python code?
s = "hello PYTHON"
new_s = ""
for char in s:
----if char.islower():
------new_s += char.upper()
----elif char.isupper():
------new_s += char.lower()
----else:
-------new_s += ' '
print(new_s)

a)

"HELLO python"

b)

"HELLO*PYTHON"

c)

"hELLO*python"

d)

"HELLO*python"

4.

What is the output of len(" Python ".strip())?

a)

10

b)

8

c)

6

d)

4

5.

Which method converts the first character of a string to uppercase and the rest to lowercase?

a)

title()

b)

upper()

c)

capitalize()

d)

casefold()

6.

If s = "InDiA", what will s.title() return?

a)

"INDIA"

b)

"india"

c)

"InDia"

d)

"India"

7.

What is the key difference between str.find() and str.index()?

a)

find() searches from the end, while index() searches from the beginning.

b)

index() returns -1 if the substring is not found, while find() raises a ValueError.

c)

find() returns -1 if the substring is not found, while index() raises a ValueError.

d)

There is no difference; they are aliases for the same method.

8.

Given s = "PYTHON", what is the output of s.lower().count('o')?

a)

0

b)

1

c)

2

d)

Error

9.

If s = "mississippi", what is the output of s.find('ss', 4)?

a)

2

b)

5

c)

4

d)

-1

10.

True or False: The len() function includes all characters, including spaces, when calculating the length of a string.

a)

True

b)

False

11.

True or False: The upper() and lower() methods modify the original string in place.

a)

True

b)

False

12.

True or False: If the substring is not found, both find() and index() will return -1.

a)

True

b)

False

13.

True or False: The capitalize() method will convert all words in a string to have their first letter in uppercase.

a)

True

b)

False

14.

Which method is used to check if a string consists only of alphabetic characters and digits (0-9)?

a)

isalpha()

b)

isdigit()

c)

isalnum()

d)

isnumeric()

15.

What will s.startswith('A', 1, 4) return if s = "ABACUS"?

a)

True

b)

False

c)

Error

d)

None

16.

For a string s to return True for s.isspace(), what must it contain?

a)

Only digits and spaces.

b)

Only whitespace characters (spaces, tabs, newlines).

c)

At least one space character.

d)

Only empty characters.

17.

Given s = "2024", which one of the following will return True?

a)

s.islower()

b)

s.isalpha()

c)

s.isalnum()

d)

s.isupper()

18.

Which method is the opposite of startswith()?

a)

ends()

b)

endswith()

c)

stopwith()

d)

stopexactly()

19.

What is the output of the following Python code? Python s = "python is fun" print(s.count('p') + s.count('n'))

a)

2

b)

1

c)

3

d)

4

20.

What is the output of the following Python code?
s = " \n "
print(s.isspace())

a)

True

b)

False

c)

None

d)

Error

21.

What is the output of the following Python code?
s = "COMP"
print(s.isupper() and s.isalpha())

a)

True

b)

False

c)

None

d)

Error

22.

What is the output of the following Python code?
s = "a1b2"

print(s.isalpha() or s.isdigit())

a)

True

b)

False

c)

Error

d)

None

23.

What is the output of the following Python code?
s = "HelloWorld"

print(s.startswith("He") and s.endswith("ld"))

a)

True

b)

False

c)

"He ld"

d)

"HeWorld"

24.

True or False: If a string contains a space, isalnum() will return False.

a)

True

b)

False

25.

True or False: The string "12.34" will return True for the isdigit() method.

a)

True

b)

False

26.

True or False: The expression islower() will return True for an empty string s = "".

a)

True

b)

False

27.

True or False: The string "class-12" will return True for isalnum().

a)

True

b)

False

28.

What is the output of the following Python code? s = "---Hello---" print(s.strip('-'))

a)

"Hello"

b)

"---Hello"

c)

"Hello---"

d)

"Hel"

29.

What is the output of the following Python code?
s = "$$Data$$"

print(s.lstrip('$'))

a)

"Data"

b)

"Data$$"

c)

"$$Data"

d)

"Data$"

30.

What is the output of the following Python code?
s = "one two one three one"

print(s.replace("one", "four", 2))

a)

"four two one three one"

b)

"four two four three one"

c)

"four two four three four"

d)

"four two one three four"

31.

What is the output of the following Python code?
l = ["C", "B", "S", "E"]
print("-".join(l))

a)

['C', 'B', 'S', 'E']

b)

"CBSE"

c)

"C-B-S-E"

d)

Error

32.

What is the output of the following Python code?
s = "A:B:C"

print(s.split(':'))

a)

('A', 'B', 'C')

b)

['A', 'B', 'C']

c)

"A B C"

d)

Error

33.

What is the output of the following Python code?
s = "name,age,city"
parts = s.split(',', 1)

print(parts[1])

a)

"age,city"

b)

"name"

c)

"city"

d)

Error

34.

What is the output of the following Python code?
s = "hello world"

print(s.partition(' '))

a)

['hello', 'world']

b)

('hello', 'world')

c)

('hello', ' ', 'world')

d)

['hello', ' ', 'world']

35.

What is the output of the following Python code?
data = ("file", "txt")
print(".".join(data))

a)

('file', '.', 'txt')

b)

"file.txt"

c)

["file", "txt"]

d)

Error

36.

If a string contains only spaces and tabs, what does strip() return?

a)

A single space character.

b)

The original string.

c)

An empty string "".

d)

None.

37.

Which of the following is true about str.partition(separator)?

a)

It returns a list of two elements.

b)

It returns a tuple of two elements.

c)

It returns a tuple of three elements.

d)

It returns a list of three elements.

38.

The maximum number of splits that str.split(delimiter, maxsplit) will perform is controlled by:

a)

delimiter

b)

maxsplit

c)

The length of the string

d)

The number of times the delimiter appears.

39.

True or False: If the delimiter for split() is not found in the string, the method returns a list containing the original string as its only element.

a)

True

b)

False

40.

True or False: The replace() method replaces all occurrences of the old substring by default.

a)

True

b)

False

41.

True or False: lstrip() and rstrip() can be used to remove specific characters, not just whitespace.

a)

True

b)

False

42.

True or False: The join() method can be used with a list containing a mix of string and integer elements.

a)

True

b)

False

43.

What is the output of the following Python code?
s = "py thon"

print(s.replace(' ', '').upper())

a)

"PY THON"

b)

"PYTHON"

c)

"PYTH ON"

d)

"PY,THON"

44.

What is the output of the following Python code?
s = "hello world"

print(s.find('o', 5, 10))

a)

4

b)

7

c)

-1

d)

6

45.

What is the output of the following Python code?
s = " *** DATA *** "
print(s.strip().strip('*').strip())

a)

"DATA"

b)

"** DATA **"

c)

"*** DATA ***"

d)

Error

46.

What is the output of the following Python code?
s = "A@B@C"
a, b, c = s.partition('@')

print(a, c)

a)

"A B@C"

b)

"A B C"

c)

"A @ B@C"

d)

"B@C A"

47.

What is the output of the following Python code?
s = "Python Program"
print(s[7:].lower().index('p'))

a)

0

b)

1

c)

7

d)

Error

48.

What is the output of the following Python code?
s = "123"
print(s.isdigit() and s.isalnum())

a)

True

b)

False

c)

None

d)

Error

49.

What is the output of the following Python code?
s = "hello"

print(s.upper()[1:-1])

a)

"ELLO"

b)

"ELL"

c)

"HELL"

d)

"HELLO"

50.

What is the output of the following Python code?
s = "COMPONENT"

print(s.replace('O', 'o', 1).lower())

a)

"cOmpONENT"

b)

"component"

c)

"compONENT"

d)

"coMPONENT"

51.

What is the output of the following Python code?
s = "Welcome to India"

print(s.split('o'))

a)

['Welc', 'me t', ' India']

b)

['Welco', 'me', 'to', 'India']

c)

['Welco', 'me to', ' India']

d)

['Welc', 'me', 't', ' India']

52.

What is the output of the following Python code?
s = "A:B:C"

print(s.find(':') * 2)

a)

2

b)

1

c)

4

d)

0

53.

What is the output of the following Python code?
s = "10"

print(s.isdigit() and s.isspace())

a)

True

b)

False

c)

Error

d)

None

54.

What is the output of the following Python code?
s = "CBSE"
new_s = s[0].lower() + s[1:]

print(new_s.title())

a)

"Cbse"

b)

"cbse"

c)

"CBSE"

d)

"C B S E"

55.

What is the output of the following Python code?
s = "Hello-World"
print(s.replace('-', ' ').title())

a)

"Hello world"

b)

"Hello-World"

c)

"Hello World"

d)

"Hello-world"

56.

What is the output of the following Python code?
s = "Data Science"
print(s.lower().startswith('d') and s.upper().endswith('E'))

a)

True

b)

False

57.

What is the output of the following Python code?
s = "Python"
print(s.lstrip('P').rstrip('n'))

a)

"ython"

b)

"Pytho"

c)

"ytho"

d)

"yth"

58.

What is the output of the following Python code?
s = "Exam@2024"
print(s.isalpha() or s.isdigit() or s.isalnum())

a)

True

b)

False

c)

Error

d)

None

59.

What is the output of the following Python code?
s = "school"
print(s.count('o', 1, 5))

a)

0

b)

1

c)

2

d)

3

60.

What is the output of the following Python code?
s = "A-B-C-D"
print(len(s.split('-', 2)))

a)

2

b)

3

c)

4

d)

5

61.

What is the output of the following Python code?
s = "a,b,c"
print(s.replace(',', ' ', 1))

a)

"a b,c"

b)

"a,b c"

c)

"a b c"

d)

"a, b, c"

62.

What is the output of the following Python code?
s = "12th_Class"
print(s.islower() and s.isupper())

a)

True

b)

False

c)

Error

d)

None

63.
  1. What is the output of the following Python code?

s = "Python"

print(s * (2 if len(s) > 5 else 1))

a)

"Python"

b)

"PythonPython"

c)

"PythonPythonPython"

d)

Error

64.
  1. What is the output of the following Python code?

s = "a,b,c"

print(s.split(','))

a)

['a', 'b', 'c']

b)

('a', 'b', 'c')

c)

"a b c"

d)

['a b c']

65.
  1. What is the output of the following Python code?

s = "hello"

print(s.index('l', 3))

a)

2

b)

3

c)

4

d)

-1

66.
  1. What is the output of the following Python code?

s = "My Data"

print(s.title().replace(' ', '#'))

a)

"My#Data"

b)

"my#data"

c)

"My Data"

d)

"My#data"

67.
  1. What is the output of the following Python code?

 

s = "a b c"

print(len(s.split()))

a)

1

b)

3

c)

5

d)

0

68.
  1. What is the output of the following Python code?

s = "PyTHoN"

print(s.swapcase().index('h'))

a)

1

b)

2

c)

3

d)

Error

69.
  1. What is the output of the following Python code?

s = "mississippi"

print(s.find('z'))

a)

0

b)

10

c)

-1

d)

Error

70.
  1. What is the output of the following Python code?

s = "hello"

print(s.endswith('lo'))

a)

True

b)

False

c)

Error

d)

None

71.
  1. What is the output of the following Python code?

s = "Class 12"

print(s.isalnum() or s.isspace())

a)

True

b)

False

c)

Error

d)

None

72.
  1. What is the output of the following Python code?

s = "abc_def"

print(s.split('_')[1].upper())

a)

def

b)

ABC

c)

DEF

d)

ABC-DEF

73.
  1. What is the output of the following Python code?

print(" ".join("ABC".lower()))

a)

"a b c"

b)

"ABC"

c)

['a', 'b', 'c']

d)

"abc"

74.
  1. What is the output of the following Python code?

s = "  Python  "

print(s.lstrip().rstrip().upper())

a)

"PYTHON"

b)

" PYTHON "

c)

" PYTHON"

d)

" PYTHON"

75.
  1. What is the output of the following Python code?

s = "DATA"

print(s.lower() == 'data')

a)

True

b)

False

c)

Error

d)

None

76.
  1. What is the output of the following Python code?

s = "abc"

s = s.replace('a', 'x')

print(s)

a)

"abc"

b)

"xbc"

c)

"xxc"

d)

"abc"

77.
  1. What is the output of the following Python code?

s = "1,2,3"

l = s.split(',')

print(len(l) + l[0].isdigit())

a)

3

b)

4

c)

5

d)

Error

78.
  1. What is the output of the following Python code?

s = "PyThOn"

print(s[::-1][-1].islower())

a)

True

b)

False

c)

Error

d)

None

79.
  1. What is the output of the following Python code?

s = "Hello"

print('l' in s and 'z' not in s)

a)

True

b)

False

c)

"I"

d)

"Z"

80.
  1. What is the output of the following Python code?

s = "programming"

print(s.capitalize().count('P'))

a)

0

b)

1

c)

2

d)

3

81.
  1. What is the output of the following Python code?

s = "A:B:C"
print(s.index(':') * s.count(':'))

a)

2

b)

1

c)

4

d)

0

82.

What will be the output of the following Python code?
s = "Programming"

print(s[3:7])

a)

gramm

b)

gram

c)

grammi

d)

rammi

83.

Given the string text = "Python" , which slice will produce the output "tho"? a) b) c) text[1:4] d) text[2:4]

a)

text[2:5]

b)

text[3:6]

c)

text[1:4]

d)

text[2:4]

84.

What does s[:5] do for the string s = "ComputerScience" ?

a)

Returns the last 5 characters.

b)

Returns the first 5 characters.

c)

Returns characters from index 5 to the end.

d)

Returns characters at indices 0 and 5.

85.

If s = "Amazing", what will s[4:] evaluate to?

a)

azin

b)

ing

c)

zing

d)

g

86.

What is the result of the following slice?

word = "Examination"
print(word[::2])

a)

Exaion

b)

Eamin

c)

Eaiai

d)

Eaiain

87.

Which slice expression reverses the string s = "reverse"?

a)

s[::1]

b)

s[-1::-1]

c)

s[:-1:-1]

d)

s[::-1]

88.

What will be the output of s[-6:-2] for s = "Education"?

a)

cat

b)

cation

c)

cati

d)

Empty String

89.

What is the output of s[8:2:-2] for s = "Mathematics"?

a)

eai

b)

iae

c)

tea

d)

Emty String

90.

What is the output of the code?
s = "Python"

print(s[1:-1:2])

a)

yhn

b)

yh

c)

yt

d)

yton

91.

For s = "CBSEClassXII", what is the output of s[12:0]?

a)

IIXCsalCSEB

b)

CBSEClassXII

c)

CBSEClass

d)

Empty String

92.

Which slice expression results in the full original string s = "Example"?

a)

s[:]

b)

s[::]

c)

s[::1]

d)

All of the Above

93.

What happens if the start index is larger than the stop index, and the step is positive?

a)

A ValueError is raised.

b)

The string is sliced in reverse.

c)

The first character is returned.

d)

An empty string is returned.

94.

s = "python" . What is the output of s[1:5:-1]?

a)

ohty

b)

ohtyp

c)

o

d)

Empty String

95.

The string data = "A1B2C3D4E5" represents five 2-character data points. Which slice will extract only the data 'B' and 'D'?

a)

data[2:8:2]

b)

data[2:6:4]

c)

data[2:9:4]

d)

data[1:8:4]

96.

Given s = "P.Y.T.H.O.N", which slice extracts all the non-dot characters: "PYTHON"?

a)

s[:-1:2]

b)

s[0:12:2]

c)

s[13::2]

d)

s[:2:]

97.

What is the correct option is it execute following statement ?
s='programming'
print(s[4:1:-1])

a)

gamn

b)

ram

c)

rgo

d)

mar

98.

What will be the output of the following code?
s = 'computer'

temp = s[::3]

print(temp[::-1])

a)

moc

b)

cpe

c)

rpu

d)

epc

99.

Find correct choice ?
s = 'programming'

temp = s[::-2]

print(temp[1:4])

a)

imr

b)

rmi

c)

gim

d)

mig

100.

What will be the output of the following code?
s = 'computer'

print(s[len(s)-3:len(s)])

a)

ter

b)

ret

c)

t

d)

te