wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Chapter 3 Test: Strings and Methods

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.
Which of the following statements contain valid Python string literals? Select all that are correct:
a)
string = 'Hello, world.'
b)
string = "1234"
c)
string = "We're #1!"
d)
string = Hello, world.
e)
string = 1234
2.
What’s the value of x after this code snippet executes? x = "Real" + "Python"
a)
'Real Python'
b)
'RealPython'
c)
' RealPython '
d)
'realpython'
3.
What’s the value of x after this code snippet executes? x = 1234 + 5
a)
12345
b)
'12345'
c)
1239
d)
'1239'
4.

Create a string literal with the following text: Spock said, "Live long and prosper." Assign the string to a variable named vulcan_logic. #hint: pay attention to your spacing

(a)  

5.
What’s the value of x after this code snippet executes? x = "1234" + 5
a)
This fails with a TypeError
b)
'12349'
c)
'12345'
d)
12345
6.

Suppose the following strings are defined: topping1 = "Peanut Butter" topping2 = "Jelly" Use string concatenation to create the string "Peanut Butter & Jelly" that is assigned to a variable named sandwich.

(a)  

7.
Characters:
a)
individual letters or symbols in a string
b)
the quotes surrounding a string
c)
joins two strings
d)
gets a single character from a string
8.
Length:
a)
gets several characters from a string at once
b)
the number of characters in a string
c)
characters in a string appear in a sequence
d)
insert a variable at a specific location in a string
9.
Delimiters:
a)
individual letters or symbols in a string
b)
the number of characters in a string
c)
joins two strings
d)
the quotes surrounding a string
10.
Escape character:
a)
/
b)
'''
c)
\
d)
~
11.
Slicing:
a)
gets several characters from a string at once
b)
the number of characters in a string
c)
joins two strings
d)
gets a single character from a string
12.
Immutable:
a)
you can’t change something; it is unchangeable
b)
insert a variable at a specific location in a string
c)
the number of characters in a string
d)
the quotes surrounding a string
13.
String interpolation
a)
you can’t change something is unchangeable
b)
insert a variable at a specific location in a string
c)
the number of characters in a string
d)
the quotes surrounding a string
14.

Characters in a string appear in a (a)   .

15.

(a)   allows you to split a string across multiple lines using the triple quotes.

16.
Given the string s = "HelloWorld", what would be the output of s[1:8]?
a)
"HelloWor"
b)
"elloWor"
c)
"elloWorl"
d)
"HelloWo"
17.
Which of the following is the correct way to check if the string str ends with the substring "ing"?
a)
str.endswith("ing")
b)
str.find("ing", -3)
c)
str == "ing"
d)
str.slice(-3) == "ing"
18.

Given a string sentence, replace every occurrence of the letter 'e' with the letter 'a'. Then, display the modified sentence. sentence = "Hello, welcome to the world of Python!"

(a)  

19.
Consider the following code: name = "Alice" age = 30 output = f"{name} is {age + 5} years old in five years." print(output) What will be the printed output?
a)
Alice is 30 years old in five years.
b)
Alice is 35 years old in five years.
c)
{name} is {age + 5} years old in five years
d)
Alice is 30 + 5 years old in five years.
20.
Using an f-string, which of the following will correctly embed the value of the variable price = 50 into the string: "The cost is 50 dollars."?
a)
f"The cost is {price dollars}."
b)
f"The cost is {price}."
c)
f"The cost is dollars {price}."
d)
f"The cost is {price} dollars."