Back

Converting between lists and strings - Answers

Q1. Get the code working in Python:

1.  monster = "Monsters are bad bad bad and we should all be very afraid."
2.  print(monster)
3. 
4.  myMonsterList=monster.split()
5.  print(myMonsterList)

Q2. The code stores a string in a variable called monster and then prints it out. It then creates a list called myMonsterList from the monster string using the join method. It then prints out the list.
Q3 - Q5. The string will be split using whatever argument we use in the split method.

Q6. Get the following code working:

monsterList=['witch','goblin','elf', 'evil pixie', 'ghost']
myString = ' '.join(monsterList)
print(myString)

Q7. This code creates a list of monsters. It then creates a new string, joining the elements in the list together using what is inside the single quotes (in this case a space). It then prints out the string.
Q8 - Q10. You join the elements together from the list to form a new string using whatever is in the single quotes.

Back