Skip to main content

Section 2.9 String operations

The + operator works with strings, but it is not addition in the mathematical sense. Instead it performs concatenation, which means joining the strings by linking them end to end. For example:

Activity 2.9.1.

Which of the following correctly prints “Apples, Oranges, Blueberries, Strawberries, Raspberries, Peaches, Plums” from the codeblock below?
fruit = "Apples, Oranges"
berries = "Blueberries, Strawberries, Raspberries"
stoneFruit = "Peaches, Plums"
  • print(berries + ", " + fruit + ", " + stoneFruit)
  • Are they in the correct order?
  • print(fruits + " " + berries + " " + stonefruit)
  • Make sure you are using the right variable names.
  • print(fruit + ", " + berries + ", " + stoneFruit)
  • This will print Apples, Oranges, Blueberries, Strawberries, Raspberries, Peaches, Plums
  • print(fruit + berries + stoneFruit)
  • Don’t forget the commas and spaces!
The * operator also works with strings by multiplying the content of a string by an integer. For example:

Activity 2.9.2.

What is printed by the following statements?
cheer = "Let's go Blue"
excl = "!"
print(cheer + excl * 2)
  • Let’s go Blue !!
  • Is there an extra space in the cheer variable?
  • Let’s go Blue!!
  • This will print Let’s go Blue!! Only excl is multiplied by 2.
  • Let’s go Blue! Let’s go Blue!
  • Think about the order of operations.
  • Lets go Blue ! Let’s go Blue !
  • Think about the order of operations, and remember that the apostrophe ' is in the string.

Activity 2.9.3.

Which of the following is not true about string operations?
  • You can combine strings using + (concatenation).
  • You can combine strings using "+". Which of the options is not true?
  • Concatenating strings automatically adds a space between the strings.
  • Concatenating joins the strings beginning to end without adding additional spaces.
  • You can use * to multiply a string by an integer.
  • You can use "*" to multiply a string by an integer.
  • The operands + and * follow the order of operations when applied to strings.
  • The operands "+" and "*" follow the order of operations when applied to strings.
You have attempted of activities on this page.