wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Basics: Strings and Loops

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What is the correct way to create a string variable in Python?

a)

string = 'Hello'

b)

string = Hello

c)

string = [Hello]

d)

string = (Hello)

2.

Which of the following will print the first three letters of the string `word = "Python"`?

a)

print(word[0:2])

b)

print(word[0:3])

c)

print(word[1:3])

d)

print(word[3:0])

3.

What is the output of `print("Great Britain".lower())`?

a)

GREAT BRITAIN

b)

great britain

c)

Great Britain

d)

greatbritain

4.

Which of the following will correctly check if the string `s = "colour"` contains the letter 'u'?

a)

if 'u' in s:

b)

if s == 'u':

c)

if s.find('u'):

d)

if s.has('u'):

5.

What does `name[2:5]` return if `name = "Alexander"`?

a)

"lex"

b)

"exa"

c)

"xan"

d)

"and"

6.

Which method would you use to remove spaces from the beginning and end of a string in Python?

a)

strip()

b)

split()

c)

remove()

d)

delete()

7.

What is the output of `print("favourite".replace("u", "o"))`?

a)

fovourite

b)

favourite

c)

favoourite

d)

favoorite

8.

Which of the following is the correct way to get the length of a string `s`?

a)

length(s)

b)

len(s)

c)

count(s)

d)

size(s)

9.

If `word = "metre"`, what does `word[-1]` return?

a)

m

b)

e

c)

r

d)

t

10.

Which of the following will convert the string `temp = "23"` to an integer?

a)

int(temp)

b)

str(temp)

c)

float(temp)

d)

temp.toInt()

11.

What is the output of the following code? ```python if 5 > 3: if 2 < 4: print("True") else: print("False") ```

a)

True

b)

False

c)

5

d)

2

12.

Which of the following is a valid Python string slicing operation?

a)

s[1:4]

b)

s(1:4)

c)

s{1:4}

d)

s<1:4>

13.

What is the output of `print("organise".upper())`?

a)

organise

b)

ORGANISE

c)

Organise

d)

Organise

14.

Which of the following will check if the string `s = "prioritise"` starts with "pri"?

a)

s.startswith("pri")

b)

s.start("pri")

c)

s.begins("pri")

d)

s.first("pri")

15.

What is the output of `print("12345"[1:4])`?

a)

123

b)

234

c)

345

d)

2345

16.

What is the output of the following code if the user enters "apple" as input?

guess = input()

while guess != "banana":

print("incorrect try again")

guess = input("Guess the fruit: ")

print("Correct!")

a)

the code will execute and end

b)

the code will say "correct"

c)

the code will print "incorrect try again" once

d)

The code prints "Wrong! Try again."

every time you type apple

17.

What is the output of the following code? ```python if "ou" in "colour": print("Contains 'ou'") else: print("Does not contain 'ou'") ```

a)

Contains 'ou'

b)

Does not contain 'ou'

c)

colour

d)

ou

18.

Which of the following will convert the string `s = "12.5"` to a floating point number?

a)

float(s)

b)

int(s)

c)

str(s)

d)

s.toFloat()

19.

What is the output of `print("Great Britain"[6:])`?

a)

Britain

b)

Great

c)

eat Britain

d)

t Britain

20.

Which of the following will check if the string `s = "metre"` is equal to "metre"?

a)

if s == "metre":

b)

if s = "metre":

c)

if s.equals("metre"):

d)

if s.equal("metre"):