wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Mastering Strings in Python

Total questions: 22

Worksheet time: 11mins

Name
Class
Date
1.

What method would you use to convert a string to lowercase?

a)

Use the .uppercase() method in Python

b)

Use the .lower() method in Python or the .toLowerCase() method in JavaScript.

c)

Use the .toUpperCase() method in Python

d)

Use the .capitalize() method in JavaScript

2.

How do you concatenate two strings in Python?

a)

Use the '&' operator to combine strings.

b)

Use the '*' operator to concatenate strings.

c)

Use the '+' operator to concatenate strings.

d)

Use the 'concat()' method to join strings.

3.

What is the output of 'Hello' + ' World'?

a)

Hello World

b)

HelloWorld

c)

Hello, World

d)

Hello World!

4.

Which escape character is used for a newline in a string?

a)

\n

b)

c)

\n

d)

/n

5.

How can you access the first character of a string?

a)

Use string.first() to get the first character.

b)

Access the first character with string.charAt(0).

c)

Use string[0] to access the first character of a string.

d)

Retrieve the first character using string.slice(1).

6.

What method would you use to find the length of a string?

a)

Count the characters manually.

b)

Apply the 'substring' function.

c)

Use the 'size' method.

d)

Use the 'length' method or function.

7.

How do you format a string using f-strings?

a)

Concatenate strings using '+' operator for formatting.

b)

Use f'string with {variable}' to format a string.

c)

Use 'string with {variable}' to format a string.

d)

Format strings with 'string.format(variable)' method.

8.

What does the method .strip() do to a string?

a)

.strip() splits a string into a list of words.

b)

.strip() removes leading and trailing whitespace from a string.

c)

.strip() adds leading and trailing whitespace to a string.

d)

.strip() converts all characters in a string to uppercase.

9.

How can you replace a substring within a string?

a)

Use the 'append' method to add a substring at the end.

b)

Use the 'insert' method to add a substring.

c)

Use the 'replace' method of the string, e.g., string.replace('old_substring', 'new_substring').

d)

Use string concatenation to replace a substring.

10.

What is the result of slicing 'Python' from index 1 to 4?

a)

ytho

b)

Pytho

c)

on

d)

yth

11.

How do you check if a substring exists within a string?

a)

Use the 'indexOf' method in C++

b)

Check the length of the string

c)

Use the 'find' method in Python

d)

Use the 'in' operator in Python or 'contains' method in Java.

12.

What is the output of 'Hello'.upper()?

a)

Hello World

b)

HELLO!

c)

hello

d)

HELLO

13.

How do you split a string into a list of words?

a)

Replace spaces with commas.

b)

Convert the string to uppercase first.

c)

Use the join() method on the string.

d)

Use the split() method on the string.

14.

What does the method .find() return if the substring is not found?

a)

0

b)

-1

c)

Error

d)

None

15.

How can you join a list of strings into a single string?

a)

Use the split() method to separate strings.

b)

Use the append() method to add strings to a list.

c)

Use the join() method, e.g., 'separator'.join(list_of_strings).

d)

Concatenate strings using the + operator.

16.

What is the purpose of the escape character '\'?

a)

To indicate the end of a string.

b)

To comment out code in programming.

c)

To allow special characters to be included in strings.

d)

To escape whitespace characters in strings.

17.

How do you format a string using the .format() method?

a)

Use placeholders in the string and call .format() with the values.

b)

Call .format() without any placeholders.

c)

Use the % operator for string formatting.

d)

Use the + operator to concatenate strings.

18.

What is the output of 'abc' * 3?

a)

abcabcabcabcabc

b)

abcabcabcabc

c)

abcabc

d)

abcabcabc

19.

How can you check if a string starts with a specific substring?

a)

Use the 'startswith()' method in Python.

b)

Use a regular expression to match the substring.

c)

Use the 'endswith()' method in Python.

d)

Check the length of the string and compare it manually.

20.

What method would you use to convert a string to title case?

a)

Use the 'title()' method in Python.

b)

Use the 'capitalize()' method in Python.

c)

Use the 'lower()' method in Python.

d)

Use the 'upper()' method in Python.

21.

s="Hello"

s[1]="k"

print(s)

What will be the output?

a)

"kello"

b)

"Hkllo"

c)

Error

22.

Strings are mutable.

a)

True

b)

False