Python String Manipulation Fundamentals Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering Python string manipulation concepts, including slicing, searching, formatting, immutability, transformations, and real-world text-processing patterns.

35 Questions
~70 minutes
1

Question 1

What does it mean when we say that Python strings are immutable?

A
Their contents cannot be changed after creation
B
They automatically grow as needed
C
They store values in multiple encodings
D
They behave like lists of characters that can be modified directly
2

Question 2

Which operator is commonly used to check if a smaller string exists within a larger one?

A
in
B
*
C
%
D
&
3

Question 3

A developer wants to remove leading and trailing spaces from user input. Which string method is typically used?

A
strip()
B
trim()
C
cut()
D
clean()
4

Question 4

Why is slicing commonly used when working with substrings?

A
It allows extraction of specific character ranges without modifying the original string
B
It automatically detects patterns
C
It changes the string in place
D
It is required for all string operations
5

Question 5

Which method is used to replace all occurrences of a substring with another substring?

A
replace()
B
swap()
C
substitute()
D
modify()
6

Question 6

A text-processing script needs to standardize case to avoid mismatches in comparisons. Why is lower() commonly used for this?

A
It converts all letters to lowercase, simplifying consistent comparisons
B
It automatically removes whitespace
C
It encrypts the text
D
It validates user input
7

Question 7

Why might a developer use join() instead of repeated string concatenation inside a loop?

A
join() is more efficient because it creates the final string in one operation
B
join() modifies each element directly
C
Concatenation is not allowed in loops
D
join() automatically sorts items
8

Question 8

Which method is often used to split a string into a list of words using whitespace as the default delimiter?

A
split()
B
break()
C
divide()
D
explode()
9

Question 9

What does the startswith() method return when a string begins with the provided prefix?

A
True
B
False
C
The prefix itself
D
A list of characters
10

Question 10

A file-import application encounters product codes that include extra spaces or formatting errors. To prepare these codes for system matching, the program removes whitespace and makes them lowercase. Why is combining strip() and lower() an effective solution?

A
Because the two methods normalize both whitespace and case, ensuring consistent comparisons
B
Because strip() validates input automatically
C
Because lower() changes numbers into letters
D
Because the two methods modify strings in place
11

Question 11

A developer needs to extract the first three characters from a tracking code but must avoid errors even when the code is shorter than expected. Why is slicing such as code[:3] a safe approach?

A
Python slicing never raises index errors when the range exceeds the string length
B
Slicing modifies the original string
C
Slicing automatically inserts missing characters
D
Slicing forces the string to a fixed length
12

Question 12

A data cleanup pipeline receives email addresses, some of which contain uppercase letters. To ensure consistent matching when searching through user records, the script converts each email to lowercase. Why is this a common pattern?

A
Email comparisons are generally case-insensitive, so lowercase ensures consistent matching
B
Lowercase increases string performance
C
Uppercase letters are invalid in email addresses
D
lower() encrypts sensitive data
13

Question 13

What is printed by the following code?

python
text = "Hello"
      print(text[1])
A
e
B
H
C
o
D
Error
14

Question 14

What does this code output when slicing the last three characters?

python
code = "A1B2C3"
      print(code[-3:])
A
2C3
B
C3
C
A1B
D
Error
15

Question 15

What is the result of calling upper()?

python
msg = "hello!"
      print(msg.upper())
A
HELLO!
B
hello!
C
Hello!
D
Error
16

Question 16

What prints from this substring replacement?

python
x = "bananas"
      print(x.replace("na", "NA"))
A
baNANAs
B
bananas
C
bNAna
D
Error
17

Question 17

What does this split operation produce?

python
line = "a,b,c"
      print(line.split(","))
A
['a', 'b', 'c']
B
['a,b,c']
C
['abc']
D
Error
18

Question 18

What is printed by this join() operation?

python
items = ["x", "y", "z"]
      print("-".join(items))
A
x-y-z
B
xyz
C
-xyz-
D
Error
19

Question 19

What is printed after using strip()?

python
val = "   hello  "
      print(val.strip())
A
hello
B
hello
C
hello
D
Error
20

Question 20

What does this code print?

python
txt = "ABCDE"
      print(txt[::2])
A
ACE
B
BD
C
ABCDE
D
Error
21

Question 21

What does endswith() return here?

python
file = "report.pdf"
      print(file.endswith(".pdf"))
A
True
B
False
C
'.pdf'
D
Error
22

Question 22

What is the result of counting substring occurrences?

python
print("mississippi".count("ss"))
A
2
B
1
C
3
D
0
23

Question 23

What does this format operation output?

python
name = "Ana"
      print(f"Hello, {name}!")
A
Hello, Ana!
B
Hello, {name}!
C
Hello name!
D
Error
24

Question 24

What happens in this reverse-string slice?

python
print("python"[::-1])
A
nohtyp
B
python
C
Error
D
n
25

Question 25

What does this code output?

python
message = "Hello"
      print(len(message))
A
5
B
4
C
6
D
Error
26

Question 26

What does splitlines() do here?

python
text = "a\nb\nc"
      print(text.splitlines())
A
['a', 'b', 'c']
B
['a\nb\nc']
C
['abc']
D
Error
27

Question 27

What is the result of calling capitalize()?

python
print("python".capitalize())
A
Python
B
PYTHON
C
python
D
Error
28

Question 28

What is the output of this concatenation?

python
print("ab" + "cd")
A
abcd
B
ab cd
C
cdab
D
Error
29

Question 29

What does zfill() do here?

python
print("42".zfill(5))
A
00042
B
42
C
42000
D
Error
30

Question 30

What happens in this formatting example?

python
print("Value: {}".format(10))
A
Value: 10
B
Value: {}
C
10 Value:
D
Error
31

Question 31

What does this membership test return?

python
print("cat" in "concatenate")
A
True
B
False
C
'cat'
D
Error
32

Question 32

What is printed by this stripping example?

python
print("xxhelloxx".strip("x"))
A
hello
B
xxhelloxx
C
hell
D
Error
33

Question 33

What is printed by swapping case?

python
print("AbC".swapcase())
A
aBc
B
ABC
C
abc
D
Error
34

Question 34

What does find() return here?

python
print("hello".find("l"))
A
2
B
1
C
-1
D
Error
35

Question 35

What is the result of using replace() to remove characters?

python
print("a-b-c".replace("-", ""))
A
abc
B
a-b-c
C
a--bc
D
Error

QUIZZES IN Python