Now that you know how to use numeric data types and variables, let's study the use of text values like words and sentences. In most programming languages, including Python, text values and variables are referred to as "strings". String variables are very similar to numeric variables, but there are a few important differences.

String Values and Assignment Statements
A string variable stores a series of characters to form a line of text. A string can hold a single character like "Y" or "N", a word or short phrase like "Stop that bus", or even longer sentences or paragraphs. In Python, a string value is always surrounded by a matched set of single quotes ('Hi') or double quotes ("there").

You can create string variables just like numeric variables. A string assignment statement starts with the variable name on the left, followed by the equals sign (=), and then the quoted string value on the right.

greeting1 = 'Hi'
greeting2 = "there"
Copy
Either style (single or double quotes) will work if you use the same style for one particular string. You can't mix single and double quotes around one string value. It is a good idea to pick one style and stick with it throughout your program. This just makes your code easier to read and understand.

You can also assign the contents of another string variable or any other expression that produces a string. In the example below, we set the greeting2 variable equal to the string value in greeting1. We also set the greetingType variable equal to the results of calling the type() function on greeting2. Can you predict the outcome? Click the "Run Code" button to see the results.
You have already seen how to print lines of text or string values to the screen using the print() function. As a reminder, the print() function can display a hard-coded string value like "I'm Alive!", the contents of a string variable, or any other expression that produces a string result. The type() function produces a string result, for example, so we could pass that result directly into the print() function.Were you able to predict the output from the above three print() statements?

Combining Strings
You can't perform math operations like subtraction or multiplication on strings, but it is possible to build larger strings by gluing together two or more smaller strings. This gluing operation is called string concatenation. In Python, you can concatenate (or combine) two or more strings using the plus (+) operator.

For example, you might want to build someone’s full name out of two variables that hold the first and last names individually. Let's use the plus (+) sign to concatenate the strings together and then pass the result directly into the print() function.Notice that the two input strings are glued directly together without any spacing. Python will not automatically add spaces between concatenated strings! Make sure your string values contain the spaces you want, or you will need to add the spaces manually. Let's build a string variable by concatenating together a mixture of hard-coded string values and string variables, and we'll make sure there is a space in-between each word.Can you see how we ensured spaces between each word in the output message?

When concatenating strings, remember to add spaces between your strings when needed. This can make the difference between a result "like this" and a result "likethis". If you don’t add spaces, your words will run together, which may or may not be what your program intended.

Combining Strings and Numbers
How would you combine a string value with a number to produce a nice message like "Congratulations, your GPA is 4.0"? Let's say the numeric part (4.0) was stored in a floating-point variable named myGPA. You might be tempted to try and use the plus sign (+) to concatenate together the string value "Congratulations, your GPA is " and the myGPA variable. Click "Run Code" in the window below to see what happens.Well, that didn't work! Instead of the hope-for message, this code produces an error message instead. A message like "TypeError: Can't convert 'float' object to str implicitly" means that you have tried to combine a floating-point value onto a string, but Python was not able to make that conversion automatically.

Python can do many things for you automatically, but if you want to concatenate a number onto a string, you need to first convert that number to a string yourself. Fortunately, you can do this easily with the str() function. When you call the str() function and pass in an integer or floating-point number like 4.0, it will return the matching string like "4.0". You can then safely glue that result onto another string. Run the code below to verify the results.Great, now we get the expected results! Remember to use the str() function any time you need to convert a number to a string.

Complex Statements
The following statement can be confusing because it has several parts. Let's take some time to make sure you understand each part.

print ("Congratulations, your GPA is " + str(myGPA) )
Copy
There are actually at least three different things happening on this single line of code! We are calling a function, str(myGPA), and getting back a string result. Then, that result is concatenated to another string using the plus sign (+) to form a single, long string. Finally, the long string gets passed into the print() statement for display on the screen.

If you prefer to break your coded logic down into a series of smaller statements, that is perfectly fine. The following example does the same thing, using multiple statements that each perform one of the steps. Run the code to verify you get the same results as the single, complex statement.

1. What are some examples of text data that might be stored in a string variable?
2. How do you write text or string values in code? What kind of quotes are used?
3. How do you use the print() statement to display the contents of string variables?
4. How do you combine strings together?
5. Does Python add a space between concatenated strings automatically? What must the programmer do if spaces are desired when concatenating?
6. How do you use the str() function to convert a number to a string?
7. How do you print a combination of string and numeric values?
8. How can complex statements be broken down into smaller steps to match the programmer's comfort level?

1. Examples of text data that might be stored in a string variable include names, addresses, sentences, paragraphs, and any other sequence of characters.

2. Text or string values are written in code using single quotes (' ') or double quotes (" ").
3. To display the contents of string variables, you can pass them as arguments to the print() function. For example, print(variable_name).
4. Strings can be combined by using the plus (+) operator. For example, "Hello" + "World" will result in "HelloWorld".
5. No, Python does not automatically add spaces between concatenated strings. If spaces are desired, the programmer must manually include them by adding the space character within the string. For example, "Hello " + "World" will result in "Hello World".
6. The str() function can be used to convert a number to a string. You can pass the number as an argument to str() and it will return the corresponding string representation. For example, str(4.0) will return "4.0".
7. To print a combination of string and numeric values, you can concatenate them by using the plus (+) operator. For example, print("Congratulations, your GPA is " + str(myGPA)) will print "Congratulations, your GPA is 4.0" if myGPA is equal to 4.0.
8. Complex statements can be broken down into smaller steps by using multiple statements that each perform one of the steps. For example, you can assign the string conversion of a number to a variable using the str() function, and then concatenate that variable with other strings in a separate statement. This allows the programmer to understand and modify each step individually.

What are the two types of numeric data types in Python?

What is the difference between an "integer" and a "float" (or "floating point")?
What are some examples of data that would fit into an integer or floating point variable?
What is the type() function used for in Python?
What are the basic mathematical operator symbols?
How do you form mathematical expressions in Python?
How do you use variables in mathematical expressions?
How is the data type of the result of a mathematical expression determined?

1. The two types of numeric data types in Python are integers and floating-point numbers (floats).

2. The main difference between an integer and a float is that an integer represents whole numbers without any fractional component, while a float represents numbers with a fractional component. Integers do not have decimal points, but floats can have decimal points.
3. Examples of data that would fit into an integer variable are the number of students in a class, a person's age, or the number of items in a shopping cart. Examples of data that would fit into a floating-point variable are a person's weight, the cost of an item, or a temperature reading.
4. The type() function in Python is used to determine the type of a given object or variable. It returns the data type of the object as a string.
5. The basic mathematical operator symbols in Python are:
- Addition: +
- Subtraction: -
- Multiplication: *
- Division: /
- Floor Division (integer division): //
- Modulus (remainder): %
- Exponentiation: **
6. Mathematical expressions in Python can be formed by using the mathematical operator symbols with the appropriate operands. For example, 2 + 3 is a simple mathematical expression that adds the numbers 2 and 3.
7. Variables can be used in mathematical expressions by simply using the variable names instead of the actual values. For example, if you have a variable x = 5, you can use x in an expression like x + 3, which would evaluate to 8.
8. The data type of the result of a mathematical expression is determined based on the types of the operands involved in the expression. If all the operands are integers, the result will be an integer. If at least one of the operands is a float, the result will be a float. If an expression involves different types (e.g., a float and an integer), Python will automatically convert the integer to a float before performing the operation.

1. Examples of text data that might be stored in a string variable could be names ("John Smith"), addresses ("123 Main St"), sentences ("The quick brown fox"), or any sequence of characters.

2. In Python, text or string values are written using single quotes ('') or double quotes (""). For example:
- Single quotes: 'Hello World'
- Double quotes: "Hello World"

3. To display the contents of string variables using the print() statement, you can pass the variable as an argument to the print() function. For example:
```python
name = "John"
print(name) # Output: John
```

4. Strings can be combined using the plus (+) operator, which is called string concatenation. For example:
```python
first_name = "John"
last_name = "Smith"
full_name = first_name + " " + last_name # Output: John Smith
```

5. No, Python does not automatically add spaces between concatenated strings. If spaces are desired, the programmer needs to include them explicitly. For example:
```python
name = "John"
greeting = "Hello, " + name + "!" # Output: Hello, John!
```

6. The str() function is used to convert a number to a string. You can pass a number as an argument to the str() function, and it will return the string representation of that number. For example:
```python
number = 10
number_as_string = str(number) # Output: "10"
```

7. To print a combination of string and numeric values, you can use string concatenation. First, convert the numeric value to a string using the str() function, and then concatenate it with the string using the plus (+) operator. For example:
```python
gpa = 4.0
message = "Congratulations, your GPA is " + str(gpa)
print(message) # Output: Congratulations, your GPA is 4.0
```

8. Complex statements can be broken down into smaller steps to match the programmer's comfort level by using multiple statements that perform each step separately. For example:
```python
gpa = 4.0
gpa_string = str(gpa)
message = "Congratulations, your GPA is " + gpa_string
print(message)
```
This breaks down the complex statement from before into three smaller statements, making it easier to understand and debug if necessary.