This notebook aims to provide a list of exercises useful to start practising your programming skills
Opening a new notebook, you should start importing the modules that are needed for the notebook to work. For instance: import numpy, import matplotlib, import datacube, etc.
In the following cells there will be some exercise with an hidden solution so that you can try to solve the problem yourself. Click on the green button 'Show Solution' to compare your solution with the one provided.
Be aware: in programming there are multiple right solutions, so it is only important that you find the same results.
i) Create a single string s3, which is the union of s1 and s2
# i)
s1 = 'Hi there'
s2 = 'my name is Ernest'
s3 = s1 + ' ' + s2 # Alternative Solution: s3 = (' ').join((s1,s2))
print(s3)
Hi there my name is Ernest
# Write here your own solution
# i)
ii) Split s3 creating a list of words, for example: ['Hi','there','my','name','is','Ernest']
# ii)
s3.split(' ')
['Hi', 'there', 'my', 'name', 'is', 'Ernest']
# Write here your own solution
# ii)
iii) Replace the name Ernest with Bob in s3
# iii)
s3 = s3.replace('Ernest','Bob')
print(s3)
Hi there my name is Bob
# Write here your own solution
# iii)
iv) Remove the word 'there ' in s3
# iv)
s3 = s3.replace('there ','') # Be aware if you write s3.replace('there','')
# there will be 2 blank spaces between 'Hi' and 'my'
print(s3)
Hi my name is Bob
# Write here your own solution
# iv)
i) Find the lenght (number of characters) of s1 and s2
# i)
s1 = 'Hello world!'
s2 = 'one two three four five'
print(len(s1),len(s2))
12 23
# Write here your own solution
# i)
ii) Create a new string s3 that has the first two letters of s1 and the last two of s2
# ii)
s3 = s1[0:2] + s2[-2:]
print(s3)
Heve
# Write here your own solution
# ii)
iii) Create a new string s4 that is the sum of s1 and s2 (leaving an empty space between s1 and s2), and count how many times the expression 'wo' appears
# iii)
s4 = s1 + ' ' + s2
print(s4)
s4.count('wo')
Hello world! one two three four five
2
# Write here your own solution
# iii)
iv) add the expression 'bye bye' in the middle of s4
# iv)
mid_point = int(len(s4)/2) # It needs to be an integer!
print(s4[:mid_point] + 'bye bye' + s4[mid_point:])
Hello world! one tbye byewo three four five
# Write here your own solution
# iv)