Start learning free

LESSON 7 OF 410 · WELCOME TO PYTHON

Strings vs Numbers in Python: What the Difference Is

Lesson Read the full explanation

  • Python distinguishes between numbers and strings!


  • In Python, "15" (with quotes) is considered a string, while 15 (without quotes) is considered a number.


  • For example, computers can perform mathematical operations on numbers, but not on strings. More on this in the next lessons.

STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · WELCOME TO PYTHON

    Strings Vs Numbers in Python, explained

    The difference between a string and a number

    Python treats 15 and "15" as two different kinds of value. Without quotes, 15 is a number (an int). With quotes, "15" is a string (str), which is text that happens to contain the characters 1 and 5. You can check with type():

    print(type(15))      # <class 'int'>
    print(type("15"))    # <class 'str'>
    print(type(15.0))    # <class 'float'>
    

    Numbers are quantities. Strings are sequences of characters. The type decides which operations are allowed and what they do.

    Single and double quotes make the same kind of string, so '15' and "15" are equal. What matters is only whether quotes are there. A common source of this confusion is input(): it always returns a string, even when the user types digits, so the result needs int() before any arithmetic.

    The same operator, different meanings

    The + operator adds numbers but joins strings. Joining strings is called concatenation. The * operator multiplies numbers but repeats a string:

    print(15 + 15)      # 30
    print("15" + "15")  # 1515
    print(15 * 3)       # 45
    print("15" * 3)     # 151515
    

    This is the fundamental difference between concatenation and numeric addition: addition combines the values, concatenation glues the text together end to end. "3" + "4" is "34", while 3 + 4 is 7.

    What does not work

    Python does not convert between the two on its own. Adding a number to a string raises an error, in either order:

    "15" + 15   # TypeError: can only concatenate str (not "int") to str
    15 + "15"   # TypeError: unsupported operand type(s) for +: 'int' and 'str'
    "15" - "5"  # TypeError: unsupported operand type(s) for -: 'str' and 'str'
    

    Strings support only a few operators (+, * with an integer and comparisons), not subtraction or division. Numbers cannot be indexed or sliced: age[0] raises TypeError: 'int' object is not subscriptable, while "2025550153"[0:3] is "202".

    Converting between them

    To mix the two, convert one side. int() and float() turn text into a number, and str() turns a number into text:

    age = 29
    print(int("15") + 15)         # 30
    print("15" + str(15))         # 1515
    print(f"{age} years old")     # 29 years old
    

    An f-string, shown on the last line, is often the simplest way to put a number inside a message, because it converts the value for you.

    Comparison also behaves differently

    Numbers compare by value, and strings compare character by character, in dictionary order. Different types are never equal:

    print("15" == 15)                    # False
    print(15 < 9)                        # False
    print("15" < "9")                    # True, because "1" comes before "9"
    print(sorted([10, 9, 2]))            # [2, 9, 10]
    print(sorted(["10", "9", "2"]))      # ['10', '2', '9']
    

    When to use a string instead of a number

    The question is whether you will do arithmetic with the value.

    • An age, a price or a count is a quantity. You might add to it, compare it or average it, so it is a number.
    • A phone number, a postal code or an ID is an identifier. You never add two phone numbers. Leading zeros matter, and int("0042") gives 42, which loses them. Characters like + or - may also appear. Store these as strings.

    The exercise, step by step

    The starting code has the two values the wrong way round:

    age = "29"
    phone = 2025550153
    
    1. Make age a number by removing the quotes: age = 29.
    2. Make phone a string by adding quotes: phone = "2025550153".
    age = 29
    phone = "2025550153"
    

    After the fix, age + 1 is 30 and type(age) is int. The phone number is text, so phone[0:3] gives its first three digits, "202".

    Key takeaways

    • Quotes make a string. Without quotes, digits are a number.
    • + adds numbers and concatenates strings. Mixing the two raises TypeError.
    • Convert with int(), float() and str(), or use an f-string.
    • Use a number for something you calculate with, and a string for an identifier or text.
    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free