Time: 10-15 minutes
Purpose: Give you practice with using strings in python.
Introduction: Try to work out the following tasks in python and run them to make sure they do what you expect.
Tasks:
- Modify the following string variable to add a period to the end of it
string1 = 'The cow jumped over the moon'
- Create a variable called string2 and set it to have the word cow (Use slicing to get it out of string1).
- Create a variable called string3 and set it to have the word moon (Use slicing to get it out of string1).
- Create a list of words out of string1 (hint, see the split() method in the documentation)
- Use .format() to fill in the missing values that it will take to make the print statement below print out the sentence βthe cow jumped over the moonβ
print('The jumped over the ')
- Repeat the above, but this time use %s instead of .format()
- Create a variable called string_count and set it to count the number of characters in a string (find a built in string method to do this)
Further learning resources:
- https://www.w3schools.com/python/python_strings.asp
- https://www.tutorialspoint.com/python/python_strings.htm
- https://docs.python.org/3.7/library/string.html
- https://docs.python.org/3.7/library/stdtypes.html#string-methods
A file showing a possible solution is available on github here