Courseiva
Data Types, Variables, Basic I/O and OperatorseasyMultiple ChoiceObjective-mapped

Why String Comparison Differs from Integer Comparison in Python

A developer writes a script to read the user's age and print 'Adult' if the age is 18 or above. The code outputs 'Adult' for age 9. What is the most likely cause?

Quick Answer

The correct answer is that the input was not converted to an integer, causing a string comparison instead of an integer comparison. In Python, when you compare two strings with operators like >=, the comparison is performed lexicographically—character by character based on their Unicode code points. So '17' >= '18' evaluates to True because the first character '1' equals '1', then '7' is greater than '8'? Actually, '7' is less than '8', so the comparison would be False—wait, the key trap is that '17' >= '18' is actually False lexicographically; the real issue is that '17' >= '18' is False, so the most likely cause is a different string comparison, such as '17' >= '18' being True only if the strings are compared incorrectly? No—the correct technical concept is that string comparison vs integer comparison in Python behaves fundamentally differently: strings compare by alphabetical order, so '9' > '18' is True because '9' > '1'. For the PCEP exam, this tests your understanding of type coercion and input handling—a common trap is forgetting to wrap input() with int(), leading to silent string comparisons that yield unexpected results. Memory tip: always convert user input to a number before comparing numeric values, or you might get a lexicographic surprise.

⚠ Common exam trap

Candidates often overlook the need to convert input from string to integer. They might focus on operator differences (>= vs >) without realizing that the input type is the root cause.

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

The input was not converted to integer.

The most likely cause is that the input was not converted to integer. In Python, the `input()` function returns a string. When comparing strings lexicographically, '9' >= '18' evaluates to True because '9' is greater than '1' in the first character. Thus, the condition `age >= '18'` (or similar) would mistakenly output 'Adult' for age 9. Converting the input to an integer with `int()` ensures numeric comparison, where 9 >= 18 is False.

Answer analysis

Option-by-option breakdown

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

  • The input was not converted to integer.

    Why this is correct

    This is correct. If the input is not converted to an integer using `int()`, the variable remains a string. Depending on how the comparison is written, this could lead to incorrect results or errors. The most straightforward explanation for the observed behavior is that the input was not converted, though the exact behavior would depend on the code.

  • The if statement lacked parentheses around the condition.

    Why it's wrong here

    This is incorrect. In Python, parentheses are not required around the condition in an if statement; they are optional. Their absence does not cause the issue described.

  • The input was converted to integer but the condition used string comparison.

    Why it's wrong here

    This is incorrect. If the input were converted to integer, string comparison would not occur. The description says 'the input was converted to integer but the condition used string comparison' — that would require converting the integer back to a string, which is unlikely.

  • The condition used >= instead of >.

    Why it's wrong here

    This is incorrect. Using `>=` instead of `>` would include 18 as adult, but it would not make age 17 evaluate as adult. For age 17, `age >= 18` is False regardless of type conversion (if properly converted). So the operator alone cannot cause the bug.

About these practice questions

Courseiva writes every PCEP question from scratch — 498 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

3 more ways 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. A developer runs the script and enters 'Alice' and '25'. What does it print?

medium
  • A.Alice is 25.0 years old.
  • B.Alice is Alice is 25 years old.
  • C.Alice is 25 years old. (with age as integer)
  • D.Alice is 25 years old.

Why D: The input() function returns strings. When print() is called with multiple arguments separated by commas, it inserts spaces between them. Since the age input '25' is not converted to a number, it is printed as a string without a decimal point. Thus, the output is 'Alice is 25 years old.'

Variation 2. A user enters 'Alice' for name and '30' for age. What is the output?

hard
  • A.Alice is 30 years old.
  • B.Alice is 30 years old
  • C.Error: cannot concatenate str
  • D.Name: Alice, Age: 30

Why A: The code `print(name + ' is ' + age + ' years old.')` concatenates the string `'Alice '`, the string `' is '`, the string `'30'`, and the string `' years old.'` using the `+` operator. In Python, the `+` operator performs string concatenation when both operands are strings, and since `input()` always returns a string, both `name` and `age` are strings, so no type error occurs. The output is exactly `Alice is 30 years old.` including the period at the end.

Variation 3. A beginner writes: x = '10'; y = 20; print(x + y). What happens?

easy
  • A.Raises TypeError
  • B.Prints 30
  • C.Prints 10 + 20
  • D.Prints 1020

Why A: Python's type system does not allow implicit concatenation of a string and an integer. The variable `x` is a string (`'10'`), and `y` is an integer (`20`). The `+` operator with these types triggers a `TypeError: unsupported operand type(s) for +: 'int' and 'str'` (or vice versa), as Python refuses to guess the programmer's intent.

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.