Font size
WorksheetsMastering Strings in Python
Total questions: 22
Worksheet time: 11mins
What method would you use to convert a string to lowercase?
Use the .uppercase() method in Python
Use the .lower() method in Python or the .toLowerCase() method in JavaScript.
Use the .toUpperCase() method in Python
Use the .capitalize() method in JavaScript
How do you concatenate two strings in Python?
Use the '&' operator to combine strings.
Use the '*' operator to concatenate strings.
Use the '+' operator to concatenate strings.
Use the 'concat()' method to join strings.
What is the output of 'Hello' + ' World'?
Hello World
HelloWorld
Hello, World
Hello World!
Which escape character is used for a newline in a string?
\n
\n
/n
How can you access the first character of a string?
Use string.first() to get the first character.
Access the first character with string.charAt(0).
Use string[0] to access the first character of a string.
Retrieve the first character using string.slice(1).
What method would you use to find the length of a string?
Count the characters manually.
Apply the 'substring' function.
Use the 'size' method.
Use the 'length' method or function.
How do you format a string using f-strings?
Concatenate strings using '+' operator for formatting.
Use f'string with {variable}' to format a string.
Use 'string with {variable}' to format a string.
Format strings with 'string.format(variable)' method.
What does the method .strip() do to a string?
.strip() splits a string into a list of words.
.strip() removes leading and trailing whitespace from a string.
.strip() adds leading and trailing whitespace to a string.
.strip() converts all characters in a string to uppercase.
How can you replace a substring within a string?
Use the 'append' method to add a substring at the end.
Use the 'insert' method to add a substring.
Use the 'replace' method of the string, e.g., string.replace('old_substring', 'new_substring').
Use string concatenation to replace a substring.
What is the result of slicing 'Python' from index 1 to 4?
ytho
Pytho
on
yth
How do you check if a substring exists within a string?
Use the 'indexOf' method in C++
Check the length of the string
Use the 'find' method in Python
Use the 'in' operator in Python or 'contains' method in Java.
What is the output of 'Hello'.upper()?
Hello World
HELLO!
hello
HELLO
How do you split a string into a list of words?
Replace spaces with commas.
Convert the string to uppercase first.
Use the join() method on the string.
Use the split() method on the string.
What does the method .find() return if the substring is not found?
0
-1
Error
None
How can you join a list of strings into a single string?
Use the split() method to separate strings.
Use the append() method to add strings to a list.
Use the join() method, e.g., 'separator'.join(list_of_strings).
Concatenate strings using the + operator.
What is the purpose of the escape character '\'?
To indicate the end of a string.
To comment out code in programming.
To allow special characters to be included in strings.
To escape whitespace characters in strings.
How do you format a string using the .format() method?
Use placeholders in the string and call .format() with the values.
Call .format() without any placeholders.
Use the % operator for string formatting.
Use the + operator to concatenate strings.
What is the output of 'abc' * 3?
abcabcabcabcabc
abcabcabcabc
abcabc
abcabcabc
How can you check if a string starts with a specific substring?
Use the 'startswith()' method in Python.
Use a regular expression to match the substring.
Use the 'endswith()' method in Python.
Check the length of the string and compare it manually.
What method would you use to convert a string to title case?
Use the 'title()' method in Python.
Use the 'capitalize()' method in Python.
Use the 'lower()' method in Python.
Use the 'upper()' method in Python.
s="Hello"
s[1]="k"
print(s)
What will be the output?
"kello"
"Hkllo"
Error
Strings are mutable.
True
False
