This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Exercises

The best way to practise the string manipulation capabilities of Python is to try them out yourself!

Here are some exercises for you to play around with your strings!

Many thanks to the following people for contributing to these exercises: Oana Cocarascu, Joe Stacey, Josiah Wang

Exercise 1

Type in the following statements and expressions into an interactive interpreter and analyse the output:

  1. string = ‘hello’
  2. string + string
  3. string * 2
  4. “bad” < “and”
  5. “Bad” < “and”
  6. “Bad” > “and”
  7. “end” < “ending”

Exercise 2

Write the string ‘a laa lalalalala’ using only ‘a’, ‘la’, ‘ ‘, and the + and * operators.

Exercise 3

Write a program that splits a sentence into words, takes the first word, and assesses whether that word starts with a capital letter

Exercise 4

Which of the following commands will give the output ‘everything is okay’

  1. ’ !!everything is okay!! ‘.strip(“ !”)
  2. ’ !!everything is okay!! ‘.strip(“ “).strip(“!”)
  3. ’ !!everything is okay!! ‘.strip(“!”).strip(“ “)
  4. ’ !!everythign is okay!! ‘.strip(“! “)

Exercise 5

Print the number of words in the following string:

string_text = 'Imperial College London is the greatest university in the world'

  • Can you do this using a single line of code?

Exercise 6

Given my_list = ['Ab', 'ba', ' a' + 're ', 'ter' + 'rible'], concatenate the elements into a string and print the hidden sentence.

Exercise 7

Why does 'money' < 'happiness' return False, while 'Money' < 'happiness' returns True?

Exercise 8

Given a string, replace all occurances of its first character with the character '_':

  • For example, 'my name is' would become '_y na_e is'

Exercise 9

The count method can be used on strings to determine the number of non-overlapping occurrences of a substring in a given string. To learn more about this method, you can type in the interpreter help(str.count). Before executing the following, can you determine what the output would be?

>>> '$100 $200 $300'.count('$')
>> '$100 $200 $300'.count('$', 5, 10)
>>> '$100 $200 $300'.count('$', 5)
>>> 'aaaa'.count('aa')

Exercise 10

Without executing the command, can you determine what the output of s.strip(" -") will be if s = ' zz-zzzz-zzz '?

Exercise 11

Split the following string into sentences and print the number of sentences:

In computer science, lexical analysis, lexing or tokenization is the process of converting a sequence of characters into a sequence of tokens (strings with an assigned and thus identified meaning). A program that performs lexical analysis may be termed a lexer or a tokenizer. .

You can assume that a sentence is delimited by a full stop (.). The expected output for the above should be 2.

Exercise 12

Split the following string London is the capital and most populous city of England and the United Kingdom into words, and print the number of words (word tokens) and the number of unique words (word types). Also print the set of unique words (vocabulary).

Exercise 13

A palindrome is a word that reads the same forwards and backwards. Example: madam, level, rotator, noon, kayak, civic, malayalam. Use Python to check whether a given string is a palindrome.

Exercise 14

Extend your palindrome program so that it also supports long phrases. Examples:

  • Top spot
  • Don’t nod.
  • I did, did I?
  • My gym
  • Step on no pets
  • Was it a cat I saw?
  • No lemon, no melon
  • Eva, can I see bees in a cave?
  • Sit on a potato pan, Otis
  • Able was I, ere I saw Elba.

All the above should be considered palindromes by your program. You can assume that we only consider English letters for determining whether a phrase is a palindrome, and ignore punctuations, spacing and case-sensitivity.