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.
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.
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.
Predict the Reported Types
What do you think happens?
What type will Python report for each value: 17, '17', 3.2, and '3.2'?
Reveal 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.
| Value | What type() identifies |
|---|---|
| 17 | integer |
| '17' | string |
| 3.2 | floating-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.
- 1Identify the first expression
In 17 + 5, both values are integers, so Python performs arithmetic.
- 2Evaluate the numeric expression
Adding the integers produces 22.
- 3Identify the second expression
In '17' + '5', both values are strings because they are enclosed in quotation marks.
- 4Evaluate 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
- 1Notice that the value is behaving differently from what you expected.
- 2Use
type()with the value in question. - 3Read the reported type rather than judging the value by its appearance.
- 4Check whether quotation marks caused the value to be a string.
- 5Compare 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
Common mistake Assuming that digits automatically make a value numeric
What it looks like'17'Why it failsQuotation marks make the value a string, regardless of the digits inside.
Do this insteadUse type() to check whether the value is a string or an integer.
Common mistake Treating 17 and '17' as interchangeable
What it looks like17 + 5 versus '17' + '5'Why it failsThe first expression uses integers and performs arithmetic; the second uses strings and concatenates their contents.
Do this insteadInspect the actual types before choosing or debugging an operation.
Common mistake Trusting what a value looks like instead of checking its type
What it looks likeAssuming a value is a number because it displays digits
Why it failsA quoted integer-looking or decimal-looking value is still a string.
Do this insteadPass the value to type() and use the reported type as the evidence.
Check Your Understanding
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.
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
- 1Use
type()to ask Python what kind of value you are working with. - 2Quotation marks always make their contents a string, even when the contents look numeric.
- 3The unquoted value 17 is an integer, while '17' is a string.
- 4Numeric and string values can behave differently in the same-looking expression.
- 5When behavior is unexpected, check the value's actual type before guessing.
At a glance
- 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.