Worked Examples: Getting Started with Strings#

These worked solutions correspond to the exercises on the Getting Started with Strings page.

How to use this notebook:

  • Try each exercise yourself first before looking at the solution

  • The code cells show both the code and its output

  • Download this notebook if you want to run and experiment with the code yourself

  • Some of these exercises produce errors - this is intentional and helps you learn what operations are valid!

Exercise: String Operations#

These exercises explore which operations work with strings and which don’t. Understanding what causes errors helps you avoid them in your own code!

Part 1: Adding Strings#

Problem: Try adding two strings together.

Solution:

'John went ' + 'for a walk'
'John went for a walk'

Explanation:

The + operator concatenates (joins) two strings together into a single string.

Key points:

  • String concatenation preserves all characters, including spaces

  • The order matters: 'John went ' + 'for a walk' is different from 'for a walk' + 'John went '

  • You can concatenate multiple strings: 'a' + 'b' + 'c' gives 'abc'

Part 2: Subtracting Strings#

Problem: Try to subtract one string from another.

Solution:

'The bird ' - 'that never flew'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 'The bird ' - 'that never flew'

TypeError: unsupported operand type(s) for -: 'str' and 'str'

Explanation:

This operation is not possible. Python doesn’t define subtraction for strings because there’s no consistent logical way to subtract one string from another.

Think about it: what would 'hello' - 'lo' mean? Should it remove 'lo' from the end? From anywhere? What if 'lo' appears multiple times?

Key concept: Not all mathematical operators work with all data types. The operators that are defined for a particular type should have clear, unambiguous meanings.

Part 3: Adding a String and a Number#

Problem: Try adding a string to a floating point number.

Solution:

'I like to eat ' + 3.14
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 'I like to eat ' + 3.14

TypeError: can only concatenate str (not "float") to str

Explanation:

You cannot directly concatenate a string and a number. Python requires you to be explicit about type conversions.

Part 4: Multiplying Two Strings#

Problem: Try multiplying two strings together.

Solution:

'Multiplication is ' * 'multiple addition'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 'Multiplication is ' * 'multiple addition'

TypeError: can't multiply sequence by non-int of type 'str'

Explanation:

You cannot multiply two strings together. There’s no meaningful way to define what 'hello' * 'world' should produce.

Part 5: Multiplying a String by an Integer#

Problem: Try multiplying a string by an integer.

Solution:

'Around the world. ' * 5
'Around the world. Around the world. Around the world. Around the world. Around the world. '

Explanation:

This does work! Multiplying a string by an integer repeats the string that many times.

Practical uses:

  • Creating separators: '-' * 40 gives '----------------------------------------'

  • Creating repeated patterns: 'abc' * 3 gives 'abcabcabc'

  • Initialising strings: ' ' * 10 gives 10 spaces

What about negative numbers or zero?

'Text' * 0
''
'Text' * -1
''

Both give empty strings (''). You can’t have negative repetitions!

What about floats?

'Text' * 2.5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[13], line 1
----> 1 'Text' * 2.5

TypeError: can't multiply sequence by non-int of type 'float'

You can only multiply strings by integers, not floats. Even 'Text' * 2.0 won’t work, even though mathematically it’s the same as 2.

Summary#

Here’s what we learned about string operations:

Operation

Example

Result

Concatenation (+)

'hello' + 'world'

'helloworld'

Subtraction (-)

'hello' - 'lo'

TypeError ✗

String + Number (+)

'value: ' + 5

TypeError ✗

String × String (*)

'hi' * 'bye'

TypeError ✗

String × Integer (*)

'hi' * 3

'hihihi'

String × Float (*)

'hi' * 2.5

TypeError ✗

Key takeaway: Understanding which operations are valid for different data types helps you write correct code and understand error messages when things go wrong.