String Functions and Methods
Revision worksheet
Fill in the Blank: Fill in the blank with the correct words.
1. To combine a list of strings into one string with a separator,
you can use the _____________ method.
2. The _____________ method returns the number of times a
substring appears in a string.
3. To check if a string starts with a certain substring, use the
_____________ method.
4. The _____________ method returns a copy of the string with
all characters in lowercase.
5. If you want to replace all occurrences of a word in a string
with another word, you should use the _____________
method.
Multiple Choice Questions: Choose the correct answer from the
choices for each question.
1. Which method would you use to split a string into a list of
words?
a) split() b) join() c) lower() d) replace()
2. What will "Python".upper() return?
a) "python" b) "PYTHON" c) "Python" d) "P Y T H O N"
3. What does the method "hello world".find("world") return?
a) 6 b) -1 c) 0 d) 5
4. What does "apple,banana,cherry".split(",") return?
a) "apple banana cherry" b) ['apple', 'banana', 'cherry']
c) "apple,banana,cherry" d) ['apple banana cherry']
5. Which method checks if all the characters in the string are
digits?
a) isdigit() b) isalpha() c) islower() d) isupper()
Case Study Question
A school wants to create a program that processes student names
and grades stored as strings. The data is received in the following
format:
data = "ABC ,95; PQR,87; XYZ,78; LMN,88"
The program must:
• Separate each student’s data.
• Split each student’s name from their grade.
• Format names so that the first letter is uppercase and the rest
are lowercase.
• Display the list of students who scored above 85.
1. Describe which string methods you would use to split the data
into individual student records and then further split each
record into name and grade. Provide example code snippets.
2. How would you make sure that each student’s name is
properly capitalized, even if the data was entered in all
capitals or all lowercase? Provide the method you would use
and an example.
3. Write a Python code snippet that prints only the names of
students who scored above 85. Use appropriate string
methods to process the data.
4. Suppose there is an extra space after some names (e.g.,
"XYZ,78"). Which method would you use to clean up the
names? Explain and show an example.
5. If the school decides to save the list of names as a single
string separated by semicolons, which string method would
you use to combine the list of names? Write a sample line of
code.
Answers
Fill in the Blanks:
1. join()
2. count()
3. startswith()
4. lower()
5. replace()
Multiple Choice:
1. a) split()
2. b) "PYTHON"
3. a) 6
4. b) ['apple', 'banana', 'cherry']
5. a) isdigit()
case study question :
1. data = "ABC ,95; PQR,87; XYZ,78; LMN,88"
students = [Link](";")
for student in students:
name, grade = [Link](",")
print(name, grade)
2. title()
eg: [Link]().title()
3. data = "ABC ,95; PQR,87; XYZ,78; LMN,88"
students = [Link](";")
for student in students:
name, grade = [Link](",")
name = [Link]()
if int(grade) > 85:
print(name)
4. [Link]()
5. names = ["Abc", "Pqr", "Xyz", "Lmn"]
result = "; ".join(names)
print(result) # Output: "Abc; Pqr; Xyz; Lmn"