Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Chapter 6

Total questions: 10

Worksheet time: 3mins

Name
Class
Date
1.

Emma is preparing a greeting message for her friend. She has two strings: one containing "Hello" and the other containing 'there'. She combines these two strings to create a message. What does the following Python Program print out? str1 = "Hello" str2 = 'there' bob = str1 + str2 print(bob)

a)

Hello there

b)

Hellothere

c)

Hello

d)

there

2.

Avery has a string representing a number: x = '40'. She wants to add 2 to this number and print the result. What does the following Python program print out? x = '40' y = int(x) + 2 print(y)

a)

x2

b)

42

c)

int402

d)

402

3.
How would you use the index operator [] to print out the letter q from the following string? x = 'From marquard@uct.ac.za'
a)
print(x[8])
b)
print(x[7])
c)
print(x[q])
d)
print(x[9])
e)
print x[-1]
4.

How would you use string slicing [:] to print out 'uct' from the following string? x = 'Noah's email is marquard@uct.ac.za'

a)

print(x[14:17])

b)

print(x[15:3])

c)

print(x[14+17])

d)

print(x[14:3])

e)

print(x[14/17])

5.
How would you print out the following variable in all upper case in Python? greet = 'Hello Bob'
a)
print(upper(greet))
b)
puts(greet.ucase);
c)
print(greet.upper())
d)
see image
6.

In a fruit shop, Noah is sorting through a basket of bananas. For each banana he picks up, he says its name aloud. What is the iteration variable in the following Python code? for letter in 'banana' : print(letter)

a)

for

b)

in

c)

print

d)

'banana'

e)

letter

7.

Hannah is making banana smoothies for a party. She knows that each smoothie requires 7 bananas. If she has a total of 6 bananas, how many bananas will she need to make 6 smoothies? print(len('banana')*7)

a)

42

b)

-1

c)

banana7

d)

banana banana banana banana banana banana banana

8.

Abigail is learning Python and is exploring different string methods to manipulate text. She comes across a list of methods and wonders which one is not a valid string method in Python. Which of the following is not a valid string method in Python?

a)

boldface()

b)

upper()

c)

lower()

d)

lstrip()

e)

startswith()

9.
What will the following Python code print out? data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008' pos = data.find('.') print(data[pos:pos+3])
a)
.ma
b)
mar
c)
uct
d)
09:14
10.

Grace is working on a project where she needs to clean up user input data. She realizes that some strings have unnecessary spaces at the beginning and end. Which of the following string methods should she use to remove whitespace from both the beginning and end of a string?

a)

wsrem()

b)

strip()

c)

split()

d)

strtrunc()