Search⌘ K
AI Features

Variables and String Methods

Explore how to assign variables properly in Python following naming conventions and avoid common pitfalls. Understand key string methods like slicing, upper(), lower(), and functions such as len(), replace(), and join(). Gain skills to manipulate and format strings effectively for engineering programming tasks.

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.

Python 3.8
x = 4 + 5
print(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

1.

Is a great descriptive variable name a valid variable name?

A.

Yes

B.

No


1 / 1

If you want to print multiple things, you will have to pass them all to the print() statement (first ...