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 programs and examining their output.
Avariable 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.
lucky=7print(lucky)7
This code creates a variable calledlucky
, and assigns to it the integer number7
. When we ask Python to tell us what is stored in the variablelucky
, it returns that number again.
We can also change what is inside a variable. For example:
changing=3print(changing)3changing=9print(changing)9different=12print(different)12print(changing)9changing=15print(changing)15
We declare a variable calledchanging
, put the integer3
in it, and verify that the assignment was properly done. Then, we assign the integer9
tochanging
, and ask again what is stored inchanging
. Python has thrown away the3
, and has replaced it with9
. Next, we create a second variable, which we calldifferent
, and put12
in it. Now we have two independent variables,different
andchanging
, 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:
red=5blue=10print(red,blue)510yellow=redprint(yellow,red,blue)5510red=blueprint(yellow,red,blue)51010
To understand this code, keep in mind that thename of the variable is always on the left side of the equals sign (the assignment operator), and thevalue of the variable is on the right side of the equals sign. First the name, then the value.
We start out declaring thatred
is5
, andblue
is10
. As you can see, you can pass several arguments toprint
to tell it to print multiple items on one line, separating them by spaces. As expected, Python reports thatred
stores5
, andblue
holds10
.
Now we create a third variable, calledyellow
. To set its value, we tell Python that we wantyellow
to be whateverred
is. (Remember: name to the left, value to the right.) Python knows thatred
is5
, so it also setsyellow
to be5
.
Now we're going to take thered
variable, and set it to the value of theblue
variable. Don't get confused — name on the left, value on the right. Python looks up the value ofblue
, and finds that it is10
. So, Python throws awayred
's old value (5
), and replaces it with10
. After this assignment Python reports thatyellow
is5
,red
is10
, andblue
is10
.
But didn't we say thatyellow
should be whatever valuered
is? The reason thatyellow
is still5
whenred
is10
, is because we only said thatyellow
should be whateverred
isat the moment of the assignment. After Python has figured out whatred
is and assigned that value toyellow
,yellow
doesn't care aboutred
any more.yellow
has a value now, and that value is going to stay the same no matter what happens tored
.
Note: The interplay between different variables in Python is, in fact, more complex than explained here. The above example works with integer numbers and with all other basic data types built into Python; the behavior oflists anddictionaries (you will encounter these complex data types later) is entirely different, though. You may read the chapter ondata types for a more detailed explanation of whatvariables really are in Python and how theirtype affects their behavior. However, it is probably sufficient for now if you just keep this in mind: whenever you are declaring variables or changing their values, you always write thename of the variable on the left of the equals sign (the assignment operator), and thevalue you wish to assign to it on the right.
For thename of the variable, it can only consist of uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the underscore character (_), and thefirst character of the namecannot be a digit. For example,1abc
and_#$ad
arenot valid variable names, while_123
anda__bc
are valid variable names.
A 'string' is simply a list of characters in order. Acharacter is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash. For example, "hello
" is a string. It is five characters long —h
,e
,l
,l
,o
. Strings can also have spaces: "hello world
" contains 11 characters: 10 letters and the space between "hello
" and "world
". 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:
>>>print('I am a single quoted string')Iamasinglequotedstring>>>print("I am a double quoted string")Iamadoublequotedstring>>>print("""I am a triple quoted string""")Iamatriplequotedstring
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 asescaping the symbol.
>>>print("So I said,\"You don't know me! You'll never understand me!\"")SoIsaid,"You don't know me! You'll never understand me!">>>print('So I said, "You don\'t know me! You\'ll never understand me!"')SoIsaid,"You don't know me! You'll never understand me!">>>print("""The double quotation mark (\") is used to indicate direct quotations.""")Thedoublequotationmark(") is used to indicate direct quotations.
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:
>>>print("This will result in only three backslashes:\\\\\\")Thiswillresultinonlythreebackslashes: \ \ \
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 program:
>>>print("Hello, world!")Hello,world!
Look at that! You've been using strings since the very beginning!
You can add two strings together using the+
operator: this is calledconcatenating them.
>>>print("Hello, "+"world!")Hello,world!
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 withHello,world!
You can also repeat strings by using the*
operator, like so:
>>>print("bouncy "*5)bouncybouncybouncybouncybouncy>>>print("bouncy "*10)bouncybouncybouncybouncybouncybouncybouncybouncybouncybouncy
The stringbouncy
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 thelen()
function, which simply takes a string and counts the number of characters in it. (len
stands for "length.") Just put the string that you want to find the length ofinside the parentheses of the function. For example:
>>>print(len("Hello, world!"))13
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:
question="What did you have for lunch?"print(question)Whatdidyouhaveforlunch?
In this program, we are creating a variable calledquestion
, and storing the string "What did you have for lunch?
" in it. Then, we just tell Python to print out whatever is inside thequestion
variable. Notice that when we tell Python to print outquestion
, there areno quotation marks around the wordquestion
: this tells Python that we are using a variable, not a string. If we put in quotation marks aroundquestion
, Python would treat it as a string, as shown below:
question="What did you have for lunch?"print("question")question
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 program so that the user can type in what they ate.
question="What did you have for lunch?"print(question)answer=raw_input()#You should use "input()" in python 3.x, because python 3.x doesn't have a function named "raw_input".print("You had "+answer+"! That sounds delicious!")
To ask the user to write something, we used a function calledraw_input()
, 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. You can also use a different function calledinput()
, which works in nearly the same way. We will learn the differences between these two functions later.
Note: In Python 3.xraw_input()
was renamed toinput()
. That is, the newinput()
function reads a line fromsys.stdin
and returns it without the trailing newline. It raisesEOFError
if the input is terminated prematurely (e.g. by pressing Ctrl+D). To get the old behavior ofinput()
, useeval(input())
.
In this program, we created a variable calledanswer
, 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 "You had
" string, and the exclamation mark at the start of the "! That sounds delicious!
" string. They help format the output and make it look nice, so that the strings don't all run together.
Take a look at this program, and see if you can figure out what it's supposed to do.
print("Please give me a number: ")number=raw_input()plusTen=number+10print("If we add 10 to your number, we get "+plusTen)
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 5, in <module> print "If we add 10 to your number, we get " + plusTenTypeError: cannot concatenate 'str' and 'int' objects
What's going on here? Python is telling us that there is aTypeError
, 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 thenumber
variable is holding a string, instead of a number. If the user enters15
, thennumber
will contain a string that is two characters long: a1
, followed by a5
. So how can we tell Python that15
should be a number, instead of a string?
Also, when printing out the answer, we are telling Python to concatenate together a string ("If we add 10 to your number, we get
") and a number (plusTen
). 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. Theint()
function will take a string and turn it into an integer, while thestr()
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:
print("Please give me a number:",)response=raw_input()number=int(response)plusTen=number+10print("If we add 10 to your number, we get "+str(plusTen))
Note: Another way of doing the same is to add a comma after the string part and then the number variable, like this:
print("If we add 10 to your number, we get ",plusTen)
or use special print formatting like this:
print("If we add 10 to your number, we get%s"%plusTen)
which alternative can be written this way, if you have multiple inputs:
plusTwenty=number+20print("If we add 10 and 20 to your number, we get%s and%s"%(plusTen,plusTwenty))
or use format()
print("If we add 10 to your number, we get{0}".format(plusTen))
That's all you need to know about strings and variables! We'll learn more about types later.
print()
: Print the output information to the userinput()
orraw_input()
: asks the user for a response, and returns that response. (Note that in version 3.xraw_input()
does not exist and has been replaced byinput()
)len()
: returns the length of a string (number of characters)str()
: returns the string representation of an objectint()
: given a string or number, returns an integerNote: #input andraw_input function accept a string as parameter. This string will be displayed on the prompt while waiting for the user input.
It is recommended to useraw_input at all times and use theint function to convert the raw string into an integer. This way we do not have to bother with error messages until the error handling chapter and will not make a security vulnerability in your code.
hello
and the number is3
you should print outhello hello hello
.)