Variables and String Methods
Dive deeper into variable assignment and string methods.
We'll cover the following...
Variables
Now is a good time to learn about assigning variables. You can store anything as a variable to access it later. Variable assignment is performed using the =
operator.
x = 4 + 5print(x)print(x + 5)
Variable names must not start with a number, must not contain spaces, and cannot be the same name as an existing keyword (names like with
, open
, in
, True
, False
, and
, or
, import
, from
, continue
, yield
, try
, except
, finally
, def
, pass
, is
, round
, max
, abs
, print
, id
, class
, bool
, int
, float
, list
, str
, dict
, set
, if
, elif
, else
, and others). Basically, if any Python interface puts bold formatting around a variable name, you are using a Python keyword, and you should pick a different variable name. Python convention is to use all lowercase letters with underscores instead of spaces like_this
. The use of CamelCase
or camelCase
is discouraged but not forbidden. Here are some valid and invalid variable names:
Valid | Invalid |
---|---|
x |
id |
list_of_values |
list |
range_of_something |
range of something |
Checking Variable Name Understanding
Is a great descriptive variable name
a valid variable name?
Yes
No
If you want to print multiple things, you will have to pass them all to the print()
...