Understanding Python Strings vs Numbers is important because it helps distinguish between text and numerical values in Python. While numbers are used in calculations, strings represent text—even when the text contains only digits.
Python identifies a value as a string when it is enclosed in quotation marks, while a value such as 1234 without quotation marks is treated as an integer.
Let’s see how Python distinguishes strings from numbers in practice.
How Python Distinguishes Strings from Numbers
The easiest way to understand the difference is to compare a number with the same value written inside quotation marks.
1234is an integer."1234"is a string.
In Python, whole numbers are represented by the int (integer) data type, while text is represented by the str (string) data type.
The value 1234 represents a number that Python can use for calculations. The value "1234" represents text made up of four characters: 1, 2, 3, and 4.
The quotation marks are what tell Python to treat the value as a string.
Example: Number vs String
number = 1234
text = "1234"
print(type(number))
print(type(text))
# Output:
# <class 'int'>
# <class 'str'>
Explanation: Here, both values contain the same digits, but Python assigns them different data types. The first value is an int, while the second is a str.
Checking the Type of a Value
When it is not obvious whether a value is a number or a string, Python’s built-in type() function can show its data type.
value1 = 50
value2 = "50"
print(type(value1))
print(type(value2))
# Output:
# <class 'int'>
# <class 'str'>
Explanation: The output confirms that 50 is an integer, while "50" is a string. This distinction becomes especially important when performing operations on these values.
Strings and Numbers Behave Differently
The difference between a string and a number becomes clearer when the values are used in an operation.
Example 1: Adding Numbers
Numbers can be added together using the + operator.
num1 = 10
num2 = 5
result = num1 + num2
print(result)
# Output:
# 15
Explanation: Because both values are numbers, Python performs mathematical addition and produces 15.
Example 2: Joining Strings
The same + operator behaves differently when it is used with strings. Instead of performing mathematical addition, it joins the strings together.
text1 = "10"
text2 = "5"
result = text1 + text2
print(result)
# Output:
# 105
Explanation: Here, Python does not calculate 10 + 5. It joins the two pieces of text, producing "105".
This is why 10 + 5 produces 15, while "10" + "5" produces "105".
Example 3: Mixing a Number and a String
A number and a string cannot normally be added directly because they represent different types of values.
number = 10
text = "5"
print(number + text)
# Output:
# TypeError
Explanation: Python raises a TypeError because it cannot perform addition between an integer and a string. The values must first be converted to compatible types.
When Numbers Are Stored as Strings
Not every value made up of digits should be stored as a number. Sometimes digits represent an identifier or other information rather than a quantity that needs mathematical calculations.
For example, a phone number is normally stored as a string:
phone_number = "9876543210"
print(type(phone_number))
# Output:
# <class 'str'>
Explanation: Although the value contains only digits, it represents a phone number rather than a quantity. Storing it as a string keeps Python from treating it as a number for mathematical operations.
Other examples include:
- Phone numbers:
"9876543210" - Postal codes:
"500001" - Employee IDs:
"1025" - Product codes:
"00125"
The last example shows an important reason for using a string. If "00125" were stored as an integer, the leading zeros would not be preserved.
Converting a Numeric String into a Number
Sometimes a value arrives as a string even though it needs to be used in a calculation. For example, data entered through a form or received from another system may contain digits as text.
In such cases, the string can be converted into a number using a built-in conversion function such as int() or float().
Example: Converting a String to an Integer
text = "25"
number = int(text)
print(number + 5)
# Output:
# 30
Explanation: The value "25" starts as a string. The int() function converts it into the integer 25, allowing Python to perform mathematical addition.
Example: Converting a String to a Float
text = "12.5"
number = float(text)
print(number + 2.5)
# Output:
# 15.0
Explanation: The float() function converts the numeric string into a floating-point number, allowing it to be used in calculations involving decimal values.
Strings vs Numbers: Quick Comparison
| Feature | String | Number |
|---|---|---|
| Example | "25" |
25 |
| Python Type | str |
Numeric type such as int |
| Represents | Text | A numerical value |
| Used directly in calculations | No | Yes |
+ operation |
Joins text | Performs addition |
| Example result | "10" + "5" → "105" |
10 + 5 → 15 |
Key Takeaways: Python Strings vs Numbers
The most important point to remember is that the characters inside a value do not always determine whether Python treats it as a number. The way a value is written as a literal determines its data type.
1234is a number and can be used in calculations."1234"is a string and represents text.- Quotation marks make the value a string.
- Numeric-looking strings can be converted into numbers when calculations are required.
- Values such as phone numbers, IDs, and codes are often better stored as strings because they represent information rather than quantities.
Understanding this distinction makes it easier to work with user input, calculations, identifiers, and other types of data in Python.