Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book

Assigning Value to Python Variables

Assigning a value to a Python variable is fairly straightforward. You put the variable name first, then an equal sign (=), and finish up with the value you are assigning to the variable. This is the syntax:

variable=value

Listing 4.13 creates the variable coffee_cup and assigns a value to it.

Listing 4.13 Assigning a Value to a Python Variable

>>> coffee_cup='coffee'
>>> print(coffee_cup)
coffee
>>>

As Listing 4.13 shows, the print function can output the variable’s value without any quotation marks around it. You can take output a step further by putting a string and a variable together as two print function arguments. The print function knows they are two distinct arguments because they are separated by a comma (,), as shown in Listing 4.14.

Listing 4.14 Displaying Text and a Variable

>>> print("My coffee cup is full of", coffee_cup)
My coffee cup is full of coffee
>>>

Formatting Variable and String Output

Using variables brings additional formatting issues. For example, the print function automatically inserts a space whenever it encounters a comma (,) in a statement. This is why you do not need to add a space at the My coffee cup is full of string’s end, as shown in Listing 4.14. Sometimes, however, you might want something else besides a space to separate a character string from a variable in the output. In such a case, you can use a separator in your statement. Listing 4.15 uses the sep separator to place an asterisk (*) in the output instead of a space.

Listing 4.15 Using Separators in Output

>>> coffee_cup='coffee'
>>> print("I love my", coffee_cup, "!", sep='*')
I love my*coffee*!
>>>

Notice you also can put variables between various strings in your print statements. In Listing 4.15, four arguments are given to the print function:

  • The string "I love my"
  • The variable coffee_cup
  • The string "!"
  • The separator designation '*'

The variable coffee_cup is between two strings. Thus, you get two asterisks (*), one between each argument to the print function. Mixing strings and variables in the print function gives you a lot of flexibility in your script’s output.

Avoiding Unassigned Variables

You cannot use a variable until you have assigned a value to it. A variable is created when it is assigned a value and not before. Listing 4.16 shows an example of this.

Listing 4.16 Behavior of an Unassigned Variable

>>> print(glass)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'glass' is not defined
>>>
>>> glass='water'
>>> print(glass)
water
>>>

When the first print(glass) statement was issued in Listing 4.16, the glass variable had not been given a value. Thus, the Python interpreter delivered an error message. Before the second time the print(glass) statement was issued, the glass variable was assigned the character string, water. Therefore, the glass variable was created and no error message was delivered for the second print(glass) statement.

Assigning Long String Values to Variables

If you need to assign a long string value to a variable, you can break it up onto multiple lines by using a couple methods. Earlier in the hour, in the “Formatting Scripts for Readability” section, you looked at using the print function with multiple lines of outputted text. The concept here is similar.

The first method involves using string concatenation (+) to put the strings together and an escape character (\) to keep a linefeed from being inserted. Listing 4.17 shows that two long lines of text were concatenated together in the long_string variable assignment.

Listing 4.17 Concatenating Text in Variable Assignment

>>> long_string="This is a really long line of text" +\
... " that I need to display!"
>>> print(long_string)
This is a really long line of text that I need to display!
>>>

Another method is to use parentheses to enclose your variable’s value. Listing 4.18 eliminates the +\ and uses parentheses () on either side of the entire long string. This makes the value into a single long character string in output.

Listing 4.18 Combining Text in Variable Assignment

>>> long_string=("This is a really long line of text"
... " that I need to display!")
>>> print(long_string)
This is a really long line of text that I need to display!
>>>

The method used in Listing 4.18 is a much cleaner method. It also helps improve the script’s readability.

More Variable Assignments

A variable’s value does not have to be only a character string—it also can be a number. In Listing 4.19, the amount of coffee consumed is assigned to the variable cups_consumed.

Listing 4.19 Assigning a Numeric Value to a Variable

>>> coffee_cup='coffee'
>>> cups_consumed=3
>>> print("I had", cups_consumed, "cups of", coffee_cup, "today!")
I had 3 cups of coffee today!
>>>

You also can assign an expression’s result to a variable. The equation 3+1 is calculated in Listing 4.20, and the resulting value 4 is assigned to the variable cups_consumed.

Listing 4.20 Assigning an Expression Result to a Variable

>>> coffee_cup='coffee'
>>> cups_consumed=3 + 1
>>> print("I had", cups_consumed, "cups of", coffee_cup, "today!")
I had 4 cups of coffee today!
>>>

You learn more about performing mathematical operations within Python scripts in Hour 5, “Using Arithmetic in Your Programs.”

Reassigning Values to a Variable

After you assign a value to a variable, the variable is not stuck with that value. It can be reassigned. Variables are called variables because their values can be varied. (Say that three times fast!)

In Listing 4.21, the variable coffee_cup has its value changed from coffee to tea. To reassign a value, you simply enter the assignment syntax with a new value at its end.

Listing 4.21 Reassigning a Variable

>>> coffee_cup='coffee'
>>> print("My cup is full of", coffee_cup)
My cup is full of coffee
>>> coffee_cup='tea'
>>> print("My cup is full of", coffee_cup)
My cup is full of tea
>>>
  • + Share This
  • 🔖 Save To Your Account