Tutorial A3 – Solutions

1 Store the following lines in a suitable Python object while maintaining the indentation format:

Twinkle, twinkle, little star,
    How I wonder what you are!
        Up above the world so high,
        Like a diamond in the sky.

Twinkle, twinkle, little star,
    How I wonder what you are

Display the object on the screen.

[1]:
# Use double quotes
poem = "Twinkle, twinkle, little star,\n    How I wonder what you are!\n        Up above the world so high,\n        Like a diamond in the sky.\n\nTwinkle, twinkle, little star,\n    How I wonder what you are"
# Do not use tabs by the way: tabs are dangerous!
print(poem)
Twinkle, twinkle, little star,
    How I wonder what you are!
        Up above the world so high,
        Like a diamond in the sky.

Twinkle, twinkle, little star,
    How I wonder what you are
[2]:
# Make this more readable
poem = (
    "Twinkle, twinkle, little star,\n"
    "    How I wonder what you are!\n"
    "        Up above the world so high,\n"
    "        Like a diamond in the sky.\n\n"
    "Twinkle, twinkle, little star,\n"
    "    How I wonder what you are"
    )
# Do not use tabs by the way: tabs are dangerous!
print(poem)
Twinkle, twinkle, little star,
    How I wonder what you are!
        Up above the world so high,
        Like a diamond in the sky.

Twinkle, twinkle, little star,
    How I wonder what you are
[3]:
# Or use triple double quotes
poem = """Twinkle, twinkle, little star,
    How I wonder what you are!
        Up above the world so high,
        Like a diamond in the sky.

Twinkle, twinkle, little star,
    How I wonder what you are"""
print(poem)
Twinkle, twinkle, little star,
    How I wonder what you are!
        Up above the world so high,
        Like a diamond in the sky.

Twinkle, twinkle, little star,
    How I wonder what you are

2 Imagine you have a structure file from the RCS Protein Database (PDB) named "HSA-glucose_complexe.pdb". Store the file name in a Python variable. Extract the base name of the file and the file extension and store both in separate variables. Now substitute the underscore (_) in the file name with a space (" "). Hint: the file extension corresponds to the four last characters of the the file name.

[4]:
file_ = "HSA-glucose_complexe.pdb"
fname = file_[:-4]
ext = file_[-4:]
fname = fname.replace("_", " ")
print(f"Filename: {fname}\nExtension: {ext}")
Filename: HSA-glucose complexe
Extension: .pdb

As an alternative solution you can use the split() method of a string. This method splits the string into substrings and returns a list. We will learn more about lists in a later exercise.

[5]:
file_.split(".")
[5]:
['HSA-glucose_complexe', 'pdb']

3 “A nut for a jar of tuna” – could this be a palindrome? How would you check with Python? Remove whitespace and convert all letters to lowercase while you do! What about the words “lived” and “devils”? Maybe they are if your join them.

[6]:
sentence = "A nut for a jar of tuna"
print(sentence[::-1])
print(sentence.replace(" ", "").lower(), "==", sentence.replace(" ", "").lower()[::-1])
anut fo raj a rof tun A
anutforajaroftuna == anutforajaroftuna
[7]:
a = "devils"
b = "lived"

print((a + b).replace(" ", "").lower(),  "==",  (a + b).replace(" ", "").lower()[::-1])
devilslived == devilslived

Advanced 1 Indentation is important in Python as we will see soon. It is recommended to use spaces instead of tabs. Imagine you have text stored as a string, where indentation is done using tabs (escape sequence \t). Use the expandtabs string method (find out how it works) to convert tabs into 4 spaces.

[8]:
code = (
    "Lorem ipsum dolor sit amet,\n"
    "\tconsectetur adipiscing elit,\n"
    "\tsed do eiusmod tempor incididunt ut labore\n"
    "et dolore magna aliqua."
    )
print(code)
Lorem ipsum dolor sit amet,
        consectetur adipiscing elit,
        sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua.
[9]:
print(code.expandtabs(4))
Lorem ipsum dolor sit amet,
    consectetur adipiscing elit,
    sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua.