User:Milanandreew/Python 3 Programming/Variables and Strings

In this section, you will be introduced to two different kinds of data in Python: variables and strings. Please follow along by running the included programmes and examining their output.

Variables
A variable is something that holds a value that may change. In simplest terms, a variable is just a box that you can put stuff in. You can use variables to store all kinds of stuff, but for now, we are just going to look at storing numbers in variables. This code creates a variable called, and assigns to it the integer number. When we ask Python to tell us what is stored in the variable, it returns that number again. We can also change what is inside a variable. For example: We declare a variable called, put the integer   in it, and verify that the assignment was properly done. Then, we assign the integer  to , and ask again what is stored in. Python has thrown away the, and has replaced it with. Next, we create a second variable, which we call, and put   in it. Now we have two independent variables,  and , that hold different information, i.e., assigning a new value to one of them is not affecting the other. You can also assign the value of a variable to be the value of another variable. For example: To understand this code, keep in mind that the name of the variable is always on the left side of the equals sign (the assignment operator), and the value of the variable on the right side of the equals sign. First the name, then the value. We start out declaring that  is , and   is. As you can see, you can pass several arguments to  to tell it to print multiple items on one line, separating them by spaces. As expected, Python reports that  stores , and   holds. Now we create a third variable, called. To set its value, we tell Python that we want  to be whatever   is. (Remember: name to the left, value to the right.) Python knows that  is , so it also sets   to be. Now we're going to take the  variable, and set it to the value of the   variable. Don't get confused — name on the left, value on the right. Python looks up the value of, and finds that it is. So, Python throws away 's old value, and replaces it with. After this assignment Python reports that  is ,   is  , and   is. But didn't we say that  should be whatever value   is? The reason that  is still   when   is , is because we only said that   should be whatever   is at the moment of the assignment. After Python has figured out what   is and assigned that value to  ,   doesn't care about   any more. has a value now, and that value is going to stay the same no matter what happens to.

A 'string' is simply a list of characters in order. A character is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash. For example,  is a string. It is five characters long —,  ,  ,  ,. Strings can also have spaces:  contains 13 characters: 10 letters, 2 punctuation marks, and the space between   and. There are no limits to the number of characters you can have in a string — you can have anywhere from one to a million or more. You can even have a string that has 0 characters, which is usually called an "empty string."

There are three ways you can declare a string in Python: single quotes ('), double quotes ("), and triple quotes ("""). In all cases, you start and end the string with your chosen string declaration. For example: You can use quotation marks within strings by placing a backslash directly before them, so that Python knows you want to include the quotation marks in the string, instead of ending the string there. Placing a backslash directly before another symbol like this is known as escaping the symbol. If you want to include a backslash in a string, you have to escape said backslash. This tells Python that you want to include the backslash in the string, instead of using it as an escape character. For example: As you can see from the above examples, only the specific character used to quote the string needs to be escaped. This makes for more readable code. To see how to use strings, let's go back for a moment to an old, familiar programme: Look at that! You've been using strings since the very beginning! You can add two strings together using the  operator: this is called concatenating them. Notice that there is a space at the end of the first string. If you don't put that in, the two words will run together, and you'll end up with You can also repeat strings by using the  operator, like so: The string  gets repeated 5 times in the 1st example and 10 times in the 2nd. If you want to find out how long a string is, you use the  function, which simply takes a string and counts the number of characters in it. ( stands for "length.") Just put the string that you want to find the length of inside the parentheses of the function. For example:

Strings and Variables
Now that you've learned about variables and strings separately, let's see how they work together. Variables can store much more than just numbers. You can also use them to store strings! Here's how: In this programme, we are creating a variable called, and storing the string   in it. Then, we just tell Python to print out whatever is inside the  variable. Notice that when we tell Python to print out, there are no quotation marks around the word  : this tells Python that we are using a variable, not a string. If we put in quotation marks around, Python would treat it as a string, as shown below: Let's try something different. Sure, it's all fine and dandy to ask the user what they had for lunch, but it doesn't make much difference if they can't respond! Let's edit this programme so that the user can type in what they ate. To ask the user to write something, we used a function called, which waits until the user writes something and presses enter, and then returns what the user wrote. Don't forget the parentheses! Even though there's nothing inside of them, they're still important, and Python will give you an error if you don't put them in. In this programme, we created a variable called, and put whatever the user wrote into it. Then, we print out a new string, which contains whatever the user wrote. Notice the extra space at the end of the  string, and the exclamation mark at the start of the  " string. They help format the output and make it look nice, so that the strings don't all run together.

Combining Numbers and Strings
Take a look at this program, and see if you can figure out what it's supposed to do. This program should take a number from the user, add 10 to it, and print out the result. But if you try running it, it won't work! You'll get an error that looks like this: Traceback (most recent call last): File "test.py", line 4, in    plusTen = number + 10 TypeError: can only concatenate str (not "int") to str What's going on here? Python is telling us that there is a, which means there is a problem with the types of information being used. Specifically, Python can't figure out how to reconcile the two types of data that are being used simultaneously: integers and strings. For example, Python thinks that the  variable is holding a string, instead of a number. If the user enters, then   will contain a string that is two characters long: a  , followed by a. So how can we tell Python that  should be a number, instead of a string? Also, when printing out the answer, we are telling Python to concatenate together a string and a number. Python doesn't know how to do that -- it can only concatenate strings together. How do we tell Python to treat a number as a string, so that we can print it out with another string? Luckily, there are two functions that are perfect solutions for these problems. The  function will take a string and turn it into an integer, while the   function will take an integer and turn it into a string. In both cases, we put what we want to change inside the parentheses. Therefore, our modified program will look like this:

That's all you need to know about strings and variables! We'll learn more about types later.

List of Learned Functions

 * : prints the output information to the user.
 * : asks the user for a response, and returns that response.
 * : returns the length of a string (number of characters).
 * : returns the string representation of an object.
 * : given a string or number, returns an integer.

Exercise
Write a programme that does the following in order:

1. Ask the user for their last name.

2. Ask the user for their first name.

3. Print out the user's first and last names in that order.

Solution