Which of the following is a valid variable name in Python?

Source: JAMB · 2023

Which of the following is a valid variable name in Python?

  1. my-variable
  2. my_variable ✓
  3. 1my_variable
  4. my_variable$
Explanation

A Python variable name may use letters, digits and underscores, and it cannot start with a digit.

my_variable follows these rules. my-variable has a hyphen, 1my_variable starts with a digit, and my_variable$ has a dollar sign. All three are invalid.

Was this explanation helpful?

Something wrong or missing? Tell us — we read every report and fix issues fast.