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. 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.
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.
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.
The string inside of the paranteheses (“What is your first name?”) is called a parameter. It tells the function how to do its job. In this case, it tells input what to print 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 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”.
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.
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.
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.