3.1. Assign a Name to a String¶
3.1.1. Concatenating (Appending) Strings¶
Computers can use names to represent anything. In the last chapter we saw that we can name
numbers (declare a variable and set its value to a number) and then do calculations using the
names for the numbers. We can also name strings and do calculations with their names, too.
A string is a sequence of characters enclosed in a pair of single, double, or triple quotes
like 'Hi'
, "How are you?"
, or '''Why can't you do that?'''
. What does it mean to
do a calculation on a string? Well, Python uses the +
symbol to concatenate strings
as shown below. Concatenate means to create a new string with all the characters in the
first string followed by all of the characters in the second string. This is also called
appending strings together.
Now try running this slightly different example.
Note
Blank spaces are not automatically added when you append two strings. If you want a blank
space in between two strings then put it there explicitly using a string with just a space
in it " "
as shown in ActiveCode1.
Try to run the example below. It should give you errors. Can you fix the errors?
Note
A string is a sequence of characters enclosed in a pair of single, double, or triple quotes.
If you start a string with a single quote you must end it with a single quote. If you start a
string with a double quote you must end it with a double quote. You must use the +
operator to append strings together.
3.1.2. Inputting Strings¶
We can use the input
function in Python to get your first and last name and then print
your full name. The input function is used like this:
input("What is your first name?")
The string inside of the paranteheses (“What is your first name?”) is called a paramter.
It tells the function how to do its job. In this case, it tells input
what to print in
order to prompt the user of the program to type something.
When the function is done getting the input from the user, it returns the string
containing whatever the user types. When the function returns a value, the function is
essentially is replaced by that value. The code input("What is your first name?")
in the
sample below will be replaced by whatever the user types - something like “Anne”.
3.1.3. Concatenating Strings and Numbers¶
You can print both strings and numbers, and you can concatenate strings using +
, but if you
try to concatenate a string and a number you will get an error. The string "5"
is stored very
differently than the number 5
in computer memory, so to concatenate the number 5
and a
string we need to convert the number into a string first. The str(num)
function will convert
a number into a string.
Note
Notice how printing the string "Fred"
prints something different than printing the value
of the variable Fred
. Printing the string "Fred"
prints the exact characters in that
string. Remember that strings are enclosed in pairs of double or single quotes and when they
are printed it will print the exact characters in the string. When you print a variable it
will print the value of that variable.
We can update our driving example to print out the cost of the trip with just one print
statement.
There is another way we can use the print
function. Instead of giving it one string that
we form with concatenation, we can give it a list of values separated by commas. Using this version
of print, we don’t have to convert costTrip
to a string because it is not being concatenated
with the string “Cost to get from Chicago to Dallas”, they are just being printed one after another.
Also notice that this version of print automatically adds a space between items that are printed.
Here that causes a problem as it means there will be a space between the $ and the cost.
Check your understanding
- The address is street
- Since street is in double quotes it will print the string street rather than the value of the variable street.
- The address is 125 Main Street
- This would be true if it was print("The address is " + street)
- It won't execute
- While this isn't printing what we probably want it to, it will print something.
Given the following code segment, what will be printed?
street = "125 Main Street"
print("The address is " + "street")
- 125 Main Street, Atlanta, GA
- This would be true if it was street + ", ".
- 125 Main Street,Atlanta, GA
- There isn't a space after the comma and one isn't added automatically.
- 125 Main Street Atlanta, GA
- What about the comma?
What will be printed when the following executes?
street = "125 Main Street"
cityState = "Atlanta, GA"
print(street + "," + cityState)