Python Strings and Files: Practice Questions
For each snippet, write the output. If it prints nothing, write “No Output”. If it raises an exception, give the exception type instead.
Use the encoding reference chart for character lookups. You may bring a printed copy to an open-note quiz. Character-code memorization and hand conversion between number bases are not required. Each snippet is independent. File questions create their own input files in the current working directory.
Question 1
s = 'hello world'
print(s[:-6])Answer
hello
Question 2
s = '12345'
xs = [1, 2, 3, 4, 5]
b = s[-1] == xs[-1]
print('b=', b)Answer
b= False
Question 3
text = 'python is awesome'
print('python' in text)
print('Python' in text)Answer
True
False
Question 4
text = 'python is awesome'
changed = text.replace('python', 'everything')
text.replace('awesome', 'short')
print(changed)
print(text)Answer
everything is awesome
python is awesome
Question 5
s = 'python is awesome'
t = s.split()
u = t[-1]
print('u=', u)Answer
u= awesome
Question 6
s = 'python\nis\tawesome'
t = s.split()
u = ' '.join(t)
print('u=', u)Answer
u= python is awesome
Question 7
s = 'python is\tawesome'
t = s.split(' ')
u = t[1]
print('u=', u)Answer
u=
Question 8
s = 'python is awesome'
t = s.split('o')
u = 'XXX'.join(t)
print('u=', u)Answer
u= pythXXXn is awesXXXme
Question 9
s = 'guido van rossum'
i = s.find(' ')
t = s[:i]
print('t=',t)Answer
t= guido
Question 10
s = 'guido van rossum'
i = s.find('G')
t = s[:i]
print('t=',t)Answer
t= guido van rossu
Question 11
with open('poem.txt', 'w', encoding='utf-8') as f:
f.write('cat\nmat\n')
with open('poem.txt', encoding='utf-8') as f:
contents = f.read()
print(repr(contents))Answer
'cat\nmat\n'
Question 12
with open('colors.txt', 'w', encoding='utf-8') as f:
f.write('red,green,blue')
with open('colors.txt', encoding='utf-8') as f:
colors = f.read().split(',')
print(colors[1])Answer
green
Question 13
with open('note.txt', 'w', encoding='utf-8') as f:
f.write('cat')
with open('note.txt', encoding='utf-8') as f:
first = f.read()
second = f.read()
print(repr(first))
print(repr(second))Answer
'cat'
''
Question 14
with open('note.txt', 'w', encoding='utf-8') as f:
f.write('old text')
with open('note.txt', 'w', encoding='utf-8') as f:
f.write('new')
with open('note.txt', encoding='utf-8') as f:
print(f.read())Answer
new
Question 15
with open('words.txt', 'w', encoding='utf-8') as f:
f.write('sun')
f.write('rise')
with open('words.txt', encoding='utf-8') as f:
print(f.read())Answer
sunrise
Question 16
with open('poem.txt', 'w', encoding='utf-8') as f:
f.write('cat\nmat\n')
with open('poem.txt', encoding='utf-8') as f:
for line in f:
print(repr(line))Answer
'cat\n'
'mat\n'
Question 17
print(chr(ord('a') - 32))Answer
A
Question 18
print(chr(ord('f') - 3))Answer
c
Question 19
s = '\x48\x45\x4C\x4C\x4F'
print('s=', s)Answer
s= HELLO
Question 20
s = 'cat\n'
print(len(s))
print(repr(s))Answer
4
'cat\n'
Question 21
import unicodedata
text = 'a\u0301'
print(len(text))
print(text == 'á')
print(unicodedata.normalize('NFC', text) == 'á')Answer
2
False
True
Question 22
data = 'cat'.encode('utf-8')
print(data)
print(data.decode('utf-8'))Answer
b'cat'
cat
Question 23
text = 'café'
data = text.encode('utf-8')
print(len(text))
print(len(data))Answer
4
5
Question 24
data = 'é'.encode('utf-8')
print(data.decode('ascii'))Answer
UnicodeDecodeError
Question 25
data = b'AB'
print(data[0])
print(data[:1])
print(data[:1].decode('ascii'))Answer
65
b'A'
A
Question 26
text = 'café'
with open('name.txt', 'w', encoding='utf-8') as f:
print(f.write(text))
with open('name.txt', 'rb') as f:
data = f.read()
print(len(data))
print(data.decode('utf-8') == text)Answer
4
5
True
Question 27
with open('note.txt', 'wb') as f:
f.write(b'Hi\n')
with open('note.txt', 'rb') as f:
data = f.read()
text = data.decode('ascii').replace('Hi', 'Hello')
print(repr(text))
print(data)Answer
'Hello\n'
b'Hi\n'
Question 28
line = 'one *two* three'
start = line.find('*')
end = line.find('*', start + 1)
print(line[start + 1:end])Answer
two
Question 29
def compile_bold(line):
start = line.find('**')
end = line.find('**', start + 2)
return line[:start] + '<b>' + line[start + 2:end] + '</b>' + line[end + 2:]
print(compile_bold('one **two** three'))Answer
one <b>two</b> three
Question 30
def marked_text(line):
start = line.find('*')
if start == -1:
return line
end = line.find('*', start + 1)
if end == -1:
return line
return line[start + 1:end]
print(marked_text('one *two* three'))
print(marked_text('one *two three'))Answer
two
one *two three
Question 31
def remove_note(text):
start = text.find('[')
end = text.find(']', start + 1)
if start == -1 or end == -1:
return text
return text[:start] + text[end + 1:]
print(remove_note('A[old]B[new]C'))
print(remove_note('A[old'))Answer
AB[new]C
A[old
Question 32
def marked_words(text):
result = ''
i = 0
while i < len(text):
start = text.find('[', i)
if start == -1:
break
end = text.find(']', start + 1)
if end == -1:
break
result += text[start + 1:end] + '!'
i = end + 1
return result
print(marked_words('a[red]b[blue]c[green'))Answer
red!blue!