For loops 2 - iterating strings

Download exercises zip

Browse file online

Let’s see some exercise about strings.

Exercise - Impertinence

Given the sequence of characters having a length multiple of 3, write some code which puts into variable triplets all the sub-sequences of three characters

Example - given:

sequence = "IMPERTINENCE"
            IMPERTINENTE

after your code, it must result:

>>> print(triplets)
['IMP', 'ERT', 'INE', 'NCE']
Show solution
[2]:

sequence = "IMPERTINENCE"  # ['IMP', 'ERT', 'INE', 'NCE']
#sequence = "CUTOUT"       # ['CUT', 'OUT']
#sequence = "O_o"          # ['O_o']

# write here


Exercise - rosco

✪✪ Given a string word and string repetitions containing only digits, write some code which puts in variable result a string containing all the characters of word repeated by the number of times reported in the corresponding position of repetitions.

Example - given:

word, repetitions = "rosco", "14323"

After your code it must result:

>>> result
'roooosssccooo'
Show solution
[3]:

word, repetitions = "rosco", "14323"      # 'roooosssccooo'
word, repetitions = "chocolate", "144232312"  # 'chhhhooooccooollaaatee'

# write here


Continue

Go on with exercises about for loops with lists

[ ]: