Courseiva

Valid Python Variable Names: Rules and Common Pitfalls

Which TWO of the following are valid variable names in Python? (Choose two.)

Quick Answer

The correct answers are _count and myVar. In Python, valid variable names must start with either a letter or an underscore, followed by any combination of letters, digits, or underscores—no spaces, hyphens, or special characters like @ or $ are allowed. This question tests your understanding of Python’s identifier rules, a core topic on the Certified Entry-Level Python Programmer PCEP exam, where you must distinguish valid names from common traps like starting with a digit (e.g., 2ndPlace) or using a reserved keyword. A frequent pitfall is forgetting that underscores are fully permitted and even encouraged for special naming conventions, such as _private or __dunder__ methods. To remember the rule, think of the mnemonic “Letter or underscore first, then digits are fine—no symbols, no spaces, no keywords in line.”

⚠ Common exam trap

Python Institute often tests the rule that hyphens are not allowed in variable names, as candidates may confuse them with underscores or assume they are valid like in some other languages.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

_count

Option B, _count, is a valid Python identifier because it begins with an underscore, which is allowed, and contains only letters and underscores. Option E, myVar, is valid because it starts with a letter and uses only alphanumeric characters with no spaces or punctuation. Option A, class, is invalid because it is a reserved keyword in Python and cannot be used as a variable name. Option C, my-var, is invalid because hyphens are not permitted in identifiers; only letters, digits, and underscores are allowed. Option D, 2ndPlace, is invalid because an identifier cannot begin with a digit.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    class

    Why it's wrong here

    class is a reserved keyword, so Python rejects it as a variable name. It tempts because it is spelled correctly and reads naturally, but keywords cannot be identifiers; a name such as class_name would be valid instead.

  • ✓

    _count

    Why this is correct

    A leading underscore is legal in Python identifiers, so _count satisfies the naming rules. Python permits underscores at the start of a name, unlike digits, which cannot begin an identifier. This makes _count a valid variable name.

  • ✗

    my-var

    Why it's wrong here

    The hyphen is parsed as a subtraction operator, so my-var is not a single identifier. It tempts because hyphens are common in everyday naming, but Python identifiers permit only letters, digits and underscores, so my_var would be valid.

  • ✗

    2ndPlace

    Why it's wrong here

    Starting with a digit makes 2ndPlace an invalid Python identifier, since names must begin with a letter or underscore, not a numeral. It is tempting because it reads naturally as a label, and such names are legal in languages permitting digit-leading identifiers or in quoted dictionary keys, but Python's tokeniser rejects it outright.

  • ✓

    myVar

    Why this is correct

    `myVar` satisfies Python's identifier rules: it begins with a letter, contains only letters and digits, and avoids reserved keywords. Python permits mixed case, so the capital V is legal, and no spaces or punctuation appear. This makes it a valid variable name under the stem's requirement.

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Which TWO of the following are valid variable names in Python? (Choose two.)

easy
  • A.total$
  • ✓ B.data1
  • C.my var
  • ✓ D._count
  • E.2nd_place

Why B: Option B, data1, is a valid Python identifier because it starts with a letter and contains only letters and digits, with no illegal characters or spaces. Option D, _count, is also valid since Python permits identifiers to begin with an underscore and it contains only letters and underscores. Option A, total$, is invalid because the dollar sign ($) is not an allowed character in Python identifiers. Option C, my var, is invalid because it contains a space, which is not permitted in a variable name. Option E, 2nd_place, is invalid because identifiers cannot begin with a digit.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.