Practice: string expressions

Quokka

An expression is a calculation of some sort. An example you've seen:

  • first_name + ' ' + last_name

This expression has three types of thing in it:

  • Variables: first_name and last_name. Each variable is a piece of memory with some data in it.
  • A constant: the ' '. That's a fixed value that will never change. ' ' will always be one space, no matter what else is going on.
  • Operators: the +s. Operators combine variables and constants. The first + combines first_name and ' '. The second + combines the result of the first one with whatever value last_name has.

Your turn.

Multiple choice

What's this output?

  • partner1 = 'Zero '
  • partner2 = "Bonnie "
  • line1 = partner2 + ' and ' + partner1 + ", sitting in a tree."
  • print( line1 )

Answer without running the code.

Saving
A
Bonnie and Zero, sitting in a tree.
B
Zero and Bonnie, sitting in a tree.
C
Bonnie  and Zero  sitting in a tree.
D
Bonnie  and Zero , sitting in a tree.

Not graded. So why do it?

Multiple choice

What's this show?

  • first = 'Dwayne'
  • second = 'Johnson'
  • moniker = 'The Rock'
  • full = first + "'" + moniker + "'" + second
  • print(full)
Saving
A
Dwayne 'The Rock' Johnson
B
Dwayne "The Rock" Johnson
C
Dwayne"The Rock"Johnson
D
Dwayne'The Rock'Johnson

Not graded. So why do it?

Multiple choice

What does this output? Try to work it out without running the code.

  • dog1 = 'Burt'
  • dog1_pronoun = 'his'
  • dog2 = "Pax"
  • dog2_pronoun = dog1_pronoun
  • dog1_fave_toy = "rattle ball"
  • dog2_fave_toy = 'foxy'
  • mid_bit = "'s favorite toy is "
  • message1 = dog1 + mid_bit + dog1_pronoun + dog1_fave_toy + "."
  • message2 = dog2 + mid_bit + dog2_pronoun + dog2_fave_toy + "."
  • print(message1)
  • print(message2)
Saving
A
Burt's favorite toy is his rattle ball.
Pax's favorite toy is his foxy.
B
Burt's favorite toy is hisrattle ball.
Pax's favorite toy is hisfoxy.
C
Burt's favorite toy is hisrattle ball.
Pax's favorite toy is hisfoxy.
D
Burt's favorite toy is his"rattle ball".
Pax's favorite toy is his'foxy'.
E
Burt's favorite toy is Cthulhu's rattle ball.
Pax's favorite toy is Cthulhu's foxy.

Not graded. So why do it?

Multiple choice

What does this output? It's a complaint about a sign painter's work.

  1. thung = "and " * 5
  2. thing = "The gap between fish "
  3. thang = 'chips is too small.'
  4. message = thing + thung + thang
  5. print(message)

You'll need to look up what * does when applied to a string. Here's a page on that.

Try to work it out without running the code.

Saving
A
and and and and The gap between fish chips is too small.
B
The gap between fish and and and and and chips is too small.
C
"The gap between fish and and and and and chips is too small."
D
It's a trap!

Not graded. So why do it?

Multiple choice

What does this output?

  • quarkbeast = 'yak ' * 4
  • jenny = len(quarkbeast)
  • print('Ya haul ' + str(jenny) + ' tons')
  • print('And waddya get.')

You might need to google Python's len() function.

Try to predict what's output without running the program.

Saving
A
You haul jenny tons
And waddya get.
B
You hauljennytons
And waddya get.
C
You haul 16 tons
And waddya get.
D
You haul16tons
And waddya get.
E

Error.

Not graded. So why do it?

Here's a line of Python:

  • lyric = "I'm just a poor boy, from a poor family."
Reflect

Why does the string constant "I'm just a poor boy, from a poor family." have double quotes?

If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.
Adela
Adela

The string has I'm in it. Would the quote in that confuse Python, if you were using single quotes?

Right! Try running this program:

  • lyric = 'I'm just a poor boy, from a poor family.'
  • print('The lyric is: ', lyric)
  • ​​​​​​​
Reflect

What happened?

If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.
Georgina
Georgina

A syntax error. The quote confused Python, like Adela said.

Computers really are stupid.

Aye, they are.