5. String Operators and Assignment Work Time¶
Quick Overview of Day
Explore how the +
and *
operators work on strings. Continue to work on the first Python assignment, focused on input/output, data types and conditionals.
CS20-CP1 Apply various problem-solving strategies to solve programming problems throughout Computer Science 20.
CS20-FP1 Utilize different data types, including integer, floating point, Boolean and string, to solve programming problems.
CS20-FP2 Investigate how control structures affect program flow.
5.1. What Does This Program Do?¶
As you saw in the WDTPD questions from yesterday, using the *
operator with strings results in something very different than using the *
operator with numeric data. Consider the following questions carefully, being sure you understand WHY they output what they do.
Note
Your teacher may choose to use the following examples as a class activity, by displaying the examples, and having you take a guess as to what you think each will do before running the code.
What will the following programs output? Why?
What will this print if you enter in a 15
? How about typing hey
?
Enter 3
and 5
when you run the code below. What will the program print? Hint: it will not be an error!
Type in your first name and last name when you run the code below. How can you fix this code to write your name properly?
5.2. Operations on Strings¶
In general, you cannot perform mathematical operations on strings, even if the
strings look like numbers. The following are illegal (assuming that message
has type string):
message - 1
"Hello" / 123
message * "Hello"
"15" + 2
Interestingly, the +
operator does work with strings, but for strings, the
+
operator represents concatenation, not addition. Concatenation means
joining the two operands by linking them end-to-end. This is the equivalent of using the Scratch join block. For example:
The output of this program is banana nut bread
. The space before the word
nut
is part of the string and is necessary to produce the space between
the concatenated strings. Take out the space and run it again.
The *
operator also works on strings. It performs repetition. For example,
'Fun'*3
is 'FunFunFun'
. One of the operands has to be a string and the
other has to be an integer.
This interpretation of +
and *
makes sense by analogy with
addition and multiplication. Just as 4*3
is equivalent to 4+4+4
, we
expect "Go"*3
to be the same as "Go"+"Go"+"Go"
, and it is. Note also in the last
example that the order of operations for *
and +
is the same as it was for arithmetic.
The repetition is done before the concatenation. If you want to cause the concatenation to be
done first, you will need to use parenthesis.
Check your understanding
- python rocks
- Concatenation does not automatically add a space.
- python
- The expression first_string + second_string is evaluated first, then the resulting string is printed.
- pythonrocks
- Yes, the two strings are glued end to end.
- Error, you cannot add two strings together.
- The + operator has different meanings depending on the operands, in this case, two strings.
intro-string-operators6: What is printed by the following statements?
first_string = "python"
second_string = "rocks"
print(first_string + second_string)
- python!!!
- Yes, repetition has precedence over concatenation
- python!python!python!
- Repetition is done first.
- pythonpythonpython!
- The repetition operator is working on the exclamation variable.
- Error, you cannot perform concatenation and repetition at the same time.
- The + and * operator are defined for strings as well as numbers.
intro-string-operators7: What is printed by the following statements?
first_string = "python"
exclamation = "!"
print(first_string + exclamation * 3)
5.3. Assignment Work Time¶
Please spend the rest of the class continuing to work on your current Python assignment (likely some kind of input/output assignment). If you are completely done that assignment, you may want to look ahead at the next assignment or ask your teacher what additional challenges you can attempt.