Concepts / Type Conversion: Changing One Type to Another

Type Conversion: Changing One Type to Another

The type() function is your tool for determining what kind of value you're working with in Python.

  • Programming

When Appearance Misleads

A value can look numeric without being a number. The value 17 is an integer, but the value '17' is a string because quotation marks change how Python treats the contents. The digits look the same to you, but Python works with two different kinds of values.

42integer"42"string
How can 42 and "42" look similar but have different types in Python?

Quotation marks always create strings, even when the characters inside them are digits or look like a decimal number.

Ask Python with type()

The type() function lets you ask Python what kind of value you are working with. Pass a value into type(), and Python reports its actual data type. This is more reliable than judging the value by its appearance. The function works with strings, integers, floats, and many other types.

type(): A built-in Python function used to determine what kind of value you are working with.

receivesreportstypeinspection function17value being inspected<class 'int'>reported type
What does type() receive, and what type information does it return for a value?

Predict the Reported Types

What do you think happens?

What type will Python report for each value: 17, '17', 3.2, and '3.2'?

  • The quoted and unquoted values have the same types because their characters look similar
  • The unquoted values are numeric, while the quoted values are strings
  • All four values are strings because type() receives visible characters
Reveal answer

Answer: The unquoted value 17 is an integer, the quoted value '17' is a string, the unquoted value 3.2 is a floating-point number, and the quoted value '3.2' is a string.

Quotation marks determine that a value is a string. Without quotation marks, 17 is an integer and 3.2 is a decimal number. With quotation marks, the contents are treated as text.

ValueWhat type() identifies
17integer
'17'string
3.2floating-point number
'3.2'string

Quotation marks make the value a string, even when the contents look numeric.

The important pattern is not the particular digits. Every value surrounded by quotation marks is reported as a string. The unquoted examples are interpreted according to their numeric form: 17 is an integer, while 3.2 is a decimal number.

Why the Difference Matters

Comparing 17 and '17'

Determine why 17 + 5 and '17' + '5' produce different results.

Identify the first expression: In 17 + 5, both values are integers, so Python performs arithmetic.

Evaluate the numeric expression: Adding the integers produces 22.

Identify the second expression: In '17' + '5', both values are strings because they are enclosed in quotation marks.

Evaluate the string expression: Python concatenates the strings, placing their characters next to each other, which produces '175'.

The values 17 and '17' may display similar digits, but they participate in different operations because one is an integer and the other is a string.

The distinction between '17' and 17 is essential: the integer participates in arithmetic, while strings can be concatenated. Check the type before assuming how an operation will behave.

Debugging Unexpected Values

  1. Notice that the value is behaving differently from what you expected.
  2. Use type() with the value in question.
  3. Read the reported type rather than judging the value by its appearance.
  4. Check whether quotation marks caused the value to be a string.
  5. Compare the actual type with the operation you intended to perform.

Suppose a value appears to be a number but does not behave like one. The first useful question is whether it is actually a string. Inspecting it with type() can reveal that quotation marks were present, turning what looks like numeric data into text. Once the actual type is known, the unexpected behavior is easier to understand.

Mistakes with Quotation Marks

  • Assuming that digits automatically make a value numeric

    Quotation marks make the value a string, regardless of the digits inside.

    Fix: Use type() to check whether the value is a string or an integer.

  • Treating 17 and '17' as interchangeable

    The first expression uses integers and performs arithmetic; the second uses strings and concatenates their contents.

    Fix: Inspect the actual types before choosing or debugging an operation.

  • Trusting what a value looks like instead of checking its type

    A quoted integer-looking or decimal-looking value is still a string.

    Fix: Pass the value to type() and use the reported type as the evidence.

Check Your Understanding

EASY

For each value below, predict whether type() will identify it as an integer, a floating-point number, or a string: 8, '8', 8.0, and '8.0'. Then verify your predictions in a Python interpreter and explain the role of quotation marks.

Hints
  • Look for quotation marks before considering the digits.
  • An unquoted whole number and an unquoted decimal number are different numeric forms.
  • Compare each prediction with the type() result.
MEDIUM

A value behaves unlike a number, even though it displays 17. Describe the first debugging check you would perform and explain what result would confirm that the value is text.

Hints
  • Use the built-in inspection tool described in this article.
  • A string type result would show that quotation marks determine how Python treats the value.

Key Takeaways

  1. Use type() to ask Python what kind of value you are working with.
  2. Quotation marks always make their contents a string, even when the contents look numeric.
  3. The unquoted value 17 is an integer, while '17' is a string.
  4. Numeric and string values can behave differently in the same-looking expression.
  5. When behavior is unexpected, check the value's actual type before guessing.

Key Takeaways

  • The type() function reveals the actual type of a Python value.
  • Quotation marks make a value a string regardless of whether its contents look like a number.
  • 17 and '17' have different types and therefore behave differently.
  • Use type() as an early debugging check when a value does not behave as expected.