NEW
Font size
WorksheetsPython Basics: Strings and Loops
Total questions: 20
Worksheet time: 10mins
What is the correct way to create a string variable in Python?
string = 'Hello'
string = Hello
string = [Hello]
string = (Hello)
Which of the following will print the first three letters of the string `word = "Python"`?
print(word[0:2])
print(word[0:3])
print(word[1:3])
print(word[3:0])
What is the output of `print("Great Britain".lower())`?
GREAT BRITAIN
great britain
Great Britain
greatbritain
Which of the following will correctly check if the string `s = "colour"` contains the letter 'u'?
if 'u' in s:
if s == 'u':
if s.find('u'):
if s.has('u'):
What does `name[2:5]` return if `name = "Alexander"`?
"lex"
"exa"
"xan"
"and"
Which method would you use to remove spaces from the beginning and end of a string in Python?
strip()
split()
remove()
delete()
What is the output of `print("favourite".replace("u", "o"))`?
fovourite
favourite
favoourite
favoorite
Which of the following is the correct way to get the length of a string `s`?
length(s)
len(s)
count(s)
size(s)
If `word = "metre"`, what does `word[-1]` return?
m
e
r
t
Which of the following will convert the string `temp = "23"` to an integer?
int(temp)
str(temp)
float(temp)
temp.toInt()
What is the output of the following code? ```python if 5 > 3: if 2 < 4: print("True") else: print("False") ```
True
False
5
2
Which of the following is a valid Python string slicing operation?
s[1:4]
s(1:4)
s{1:4}
s<1:4>
What is the output of `print("organise".upper())`?
organise
ORGANISE
Organise
Organise
Which of the following will check if the string `s = "prioritise"` starts with "pri"?
s.startswith("pri")
s.start("pri")
s.begins("pri")
s.first("pri")
What is the output of `print("12345"[1:4])`?
123
234
345
2345
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!")
the code will execute and end
the code will say "correct"
the code will print "incorrect try again" once
The code prints "Wrong! Try again."
every time you type apple
What is the output of the following code? ```python if "ou" in "colour": print("Contains 'ou'") else: print("Does not contain 'ou'") ```
Contains 'ou'
Does not contain 'ou'
colour
ou
Which of the following will convert the string `s = "12.5"` to a floating point number?
float(s)
int(s)
str(s)
s.toFloat()
What is the output of `print("Great Britain"[6:])`?
Britain
Great
eat Britain
t Britain
Which of the following will check if the string `s = "metre"` is equal to "metre"?
if s == "metre":
if s = "metre":
if s.equals("metre"):
if s.equal("metre"):
