Playing as a guest? to earn XP and track your progress.
๐ค String Manipulation Quiz
๐ค String Manipulation
For word = "COMPUTER", what does print(word[0], word[5], word[-1], word[-3]) display?
O T R U
C T R T
C T E T
C U R E
Index 0 is C, 5 is T, -1 is R and -3 is T (C0 O1 M2 P3 U4 T5 E6 R7).
What happens when you run name = "Rahul" and then name[0] = "M"?
name becomes 'MRahul'
name becomes 'Mahul'
IndexError
TypeError: 'str' object does not support item assignment
Strings are immutable. Build a new string instead: name = "M" + name[1:].
What is printed after this code?
s = "hello" s.upper() print(s)
Hello
Error
hello
HELLO
upper() returns a NEW string; the result was not stored, so s is unchanged.
What does 'traversing a string' mean?
Visiting each character one by one, usually with a loop
Converting it to a list
Deleting spaces
Reversing it
Traversing means visiting each character of the string one by one.
Which loop gives the INDEX of each character of s?
while ch in s:
for i in range(len(s)):
for ch in s:
for i in s.index():
for i in range(len(s)) gives each index i; the character is s[i].
What does this code print?
s = "PYTHON" for i in range(len(s) - 1, -1, -1): print(s[i], end="")
P Y T H O N
NOHTY
PYTHON
NOHTYP
The indices go from 5 down to 0, so the string is printed in reverse.
What is the output of print("10" + "20")?
30
10 20
1020
Error
- on two strings joins them (concatenation); it does not add numbers.
What is the result of "Age: " + 16?
TypeError: can only concatenate str (not "int") to str
16
Age: 16
Age: 1616
Both operands of + must be strings; use "Age: " + str(16).
What is the output of print(3 * "-=")?
3-=
-=-=-=
-=3
Error
- with a string and an integer repeats the string 3 times.
What is the result of "abc" * 0?
"abc"
Error
0
"" (an empty string)
Repeating a string 0 times gives an empty string.
For s = "Informatics", what does print("For" in s) display?
1
Error
False
True
Membership is case-sensitive: "for" is in s but "For" is not.
What is the output of print("apple" < "apricot")?
True
False
None
Error
The first difference is at index 2: 'p' (112) < 'r' (114).
What is the output of print("10" < "9")?
False
True
10
Error
These are strings compared character by character: '1' (49) < '9' (57).
What is the output of print("abc" < "abcd")?
Error
False
None
True
When one string is a prefix of the other, the shorter one is smaller.
What is the output of print('Python' > 'python')?
None
Error
False
True
'P' is 80 and 'p' is 112, so 'Python' is smaller than 'python'.
What do ord("A") and chr(97) return?
65 and 'a'
65 and 'A'
97 and 'A'
'A' and 97
ord() gives a character's Unicode value (A = 65); chr() gives the character for a value (97 = 'a').
In string comparison, which order is correct (smallest first)?
Small letters < capitals < digits
Digits < capitals < small letters
Capitals < digits < small letters
Digits < small letters < capitals
Digits start at 48, capitals at 65 and small letters at 97.
For s = "PROGRAMMING", what is s[3:7]?
'RAMM'
'GRAMM'
'OGRA'
'GRAM'
Indices 3 to 6 (7 is excluded): G, R, A, M.
For s = "PROGRAMMING", what is s[7:]?
'ING'
'AMMING'
'MING'
'MMING'
It runs from index 7 to the end: M, I, N, G.
For s = "PROGRAMMING", what is s[::2]?
'PORMIG'
'POGAMN'
'RGAMN'
'PROGRAM'
Every 2nd character from index 0: indices 0, 2, 4, 6, 8, 10 โ P, O, R, M, I, G.
For s = "PROGRAMMING", what is s[-7:-3]?
'GRAM'
'RAMM'
'RAMMI'
'AMMI'
Indices -7 to -4 are R, A, M, M (-3 is excluded).
Which slice reverses the whole string s?
s[:-1]
s[::1]
s[-1:0]
s[::-1]
A step of -1 with default start and stop goes from the end to the beginning.
For s = "PROGRAMMING", what is s[5:2]?
IndexError
'RGO'
'' (empty string)
'ARG'
With the default positive step, you cannot move forward from 5 to 2, so the result is empty.
For s = "PROGRAMMING", what is s[8:2:-2]?
'IMR'
'IMRG'
'IMA'
'NMA'
Going backwards from index 8 in steps of 2: indices 8, 6, 4 โ I, M, R (index 2 is excluded).
For s = "Python", which statement is correct?
s[5:20] gives an IndexError
s[20] gives an IndexError, but s[5:20] returns 'n'
Both return ''
s[20] and s[5:20] both give errors
Indexing out of range raises IndexError, but slicing simply stops at the end of the string.
For any string s and integer n, s[:n] + s[n:] always equals:
An empty string
s twice
s reversed
s
Joining the two slices always gives back the full string.
For s = "INFORMATICS", what is s[2:6]?
'FORMA'
'NFOR'
'FORM'
'ORMA'
Indices 2, 3, 4, 5 give F, O, R, M.
For s = "INFORMATICS", what is s[::3]?
'IOAC'
'NRAI'
'IRTS'
'IFMTC'
Indices 0, 3, 6, 9 give I, O, A, C.
For s = "INFORMATICS", what is s[7:1:-2]?
'TMOF'
'TMO'
'TAR'
'IAR'
Going backwards from index 7 in steps of 2: indices 7, 5, 3 give T, M, O.
For s = "COMPUTER SCIENCE", what is s[3:8]?
'UTER'
'MPUTE'
'PUTER '
'PUTER'
Indices 3 to 7 give P, U, T, E, R.
What is the output of print("CBSE"[::-1])?
EBSC
SBCE
ESBC
CBSE
A step of -1 reverses the string.
What is "hELLO wORLD".capitalize()?
'Hello world'
'Hello World'
'hello world'
'HELLO WORLD'
capitalize() makes only the first character of the string capital and the rest lowercase.
What is "good morning india".title()?
'good Morning India'
'Good Morning India'
'Good morning india'
'GOOD MORNING INDIA'
title() capitalises the first letter of every word.
What is "PyThOn".swapcase()?
'PyThOn'
'python'
'PYTHON'
'pYtHoN'
swapcase() changes uppercase letters to lowercase and lowercase letters to uppercase.
What does "banana".count("a") return?
6
2
3
1
The letter 'a' occurs 3 times in 'banana'.
What does "banana".find("an") return?
1
-1
3
2
find() returns the lowest index where the substring occurs; 'an' first starts at index 1.
What does "banana".find("an", 2) return?
-1
3
5
1
The search starts at index 2, and the next 'an' begins at index 3.
What does "hello".find("z") return?
0
ValueError
None
-1
find() returns -1 when the substring is not found.
How do find() and index() differ when the substring is NOT found?
Both return -1
find() raises ValueError; index() returns -1
find() returns -1; index() raises ValueError
Both raise ValueError
Both return the lowest index when the substring is found; when it is missing, find() gives -1 but index() raises ValueError.
What does "report.pdf".endswith(".pdf") return?
True
'.pdf'
False
7
endswith() returns True if the string ends with the given substring.
What does "Hello World".isalpha() return?
None
False
True
Error
The space is not a letter, so isalpha() is False.
What does "-5".isdigit() return?
True
Error
-5
False
'-' is not a digit, so isdigit() is False.
What does "abc123".islower() return?
Error
None
True
False
Digits are ignored; all the letters are lowercase, so the result is True.
Which string makes isalnum() return False?
"Class 11"
"2025"
"Class11"
"abc"
isalnum() needs letters or digits only; the space in "Class 11" makes it False.
What does "".isalpha() return?
None
False
Error
True
All the is...() methods return False for an empty string.
What is " Hi there ".strip()?
' Hi there'
'Hi there'
'Hithere'
'Hi there'
strip() removes spaces only from the two ends; the spaces between words stay.
For s = " Hi there ", what does s.lstrip() return?
'Hithere'
' Hi there'
'Hi there '
'Hi there'
lstrip() removes only the leading (left) spaces.
What is "www.cbse.gov.in".lstrip("w.")?
'cbse.gov.in'
'cbse.gov'
'www.cbse.gov.in'
'.cbse.gov.in'
lstrip("w.") removes any leading 'w' and '.' characters from the left end.
What is "banana".replace("a", "o")?
'bnn'
'bonono'
'bonana'
'banano'
replace() replaces ALL occurrences of the old substring.
What does "10-20-30".split("-") return?
'102030'
[10, 20, 30]
('10', '-', '20-30')
['10', '20', '30']
split(sep) breaks the string at every separator and returns a list of strings.
What does "a,b,,c".split(",") return?
['a', 'b', 'c']
('a', ',', 'b,,c')
['a', 'b', '', 'c']
['a,b', 'c']
The two commas next to each other produce an empty string between them.
What is "-".join("2026")?
'2-0-2-6'
'-2026-'
'2026'
Error
join() puts the separator between the items of the sequence โ here, each character.
Which is the correct way to join the list lst with '-'?
lst + "-"
"-".join(lst)
join(lst, "-")
lst.join("-")
The separator comes first: "-".join(lst). All items must be strings.
What does "a=b=c".partition("=") return?
['a', 'b', 'c']
('a', 'b=c')
('a=b', '=', 'c')
('a', '=', 'b=c')
partition() splits at the FIRST separator into a 3-part tuple (before, separator, after).
What does "hello".partition("@") return?
['hello']
Error
('hello', '', '')
('', '', 'hello')
When the separator is not found, the result is (whole string, '', '').
Which statement comparing split() and partition() is correct?
split() splits at every occurrence and returns a list; partition() splits at the first occurrence and returns a 3-tuple
partition() splits at the last occurrence
split() returns a tuple; partition() returns a list
Both keep the separator in the result
split() returns a list without the separator; partition() returns (before, separator, after) at the first occurrence.
For s = "Python Programming", what does print(s.count("o"), s.find("P", 1), s.upper()[7:10]) display?
3 7 ROG
2 7 PRO
2 0 PRO
2 7 Pro
'o' occurs twice; the next 'P' after index 0 is at 7; the uppercase string's indices 7-9 are P, R, O.
What is printed by this code?
msg = "Board@2026" new = "" for ch in msg: if ch.isupper(): new += ch.lower() elif ch.islower(): new += ch.upper() elif ch.isdigit(): new += str(int(ch) + 1) else: new += "#" print(new)
BOARD#2026
bOARD@3137
bOARD#2026
bOARD#3137
Letters swap case, each digit increases by 1 (2026 becomes 3137), and '@' becomes '#'.
What does the palindrome check s.lower() == s.lower()[::-1] print for "Madam"?
Madam is not a palindrome
Error
Madam is a palindrome
Nothing
'madam' reversed is 'madam'; converting to lowercase first makes the check ignore case.
What does the initials program print for "mohandas karamchand gandhi"?
for part in name.split(): initials += part[0].upper() + "."
M.K.G.
M.K.G
MKG
m.k.g.
It takes the first letter of each word, makes it uppercase and adds a dot.