Execute Program

Python in Detail: Repr

Welcome to the Repr lesson!

This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!

  • We've seen the built-in str function, which converts non-string values into strings. This lesson covers repr, which also converts values into strings, but these strings are specifically meant for programmers. For some values, like integers, repr behaves exactly like str.

  • >
    repr(5)
    Result:
  • >
    repr(42)
    Result:
    '42'Pass Icon
  • For strings, repr and str behave differently. The str of a string is exactly the same string, but the repr of a string contains quotes.

  • >
    repr("Hello")
    Result:
  • >
    repr("Amir")
    Result:
    "'Amir'"Pass Icon
  • >
    (repr("Hello"), str("Hello"))
    Result:
    ("'Hello'", 'Hello')Pass Icon
  • repr returns the canonical or official representation of a value. Usually, repr returns a string that gives us the original value when passed to eval. In other words, repr usually gives us valid Python code that will recreate the original value.

  • >
    eval(repr("Amir"))
    Result:
  • >
    eval(repr("Amir")) == "Amir"
    Result:
    TruePass Icon
  • The difference between repr and str may seem minor at first, but it's important. For example, str(10), str("10"), and str(Decimal(10)) are all the same. When we print them to the console, we can't tell the difference.

  • >
    from decimal import Decimal

    print("The value is:", str(10))
    print("The value is:", str("10"))
    print("The value is:", str(Decimal(10)))
    console output
  • >
    from decimal import Decimal

    str(10) == str("10") == str(Decimal(10))
    Result:
    TruePass Icon
  • This can cause problems when debugging. We might print a variable out with str, see that it contains 10, and not understand why the code isn't working. The problem is that the we think the value is the integer 10, but it's actually "10", or maybe Decimal(10). Those differences are hidden from us when using str.

  • Now here's the repr equivalent, where all three values are clearly distinct.

  • >
    from decimal import Decimal

    print("The value was:", repr(10))
    print("The value was:", repr("10"))
    print("The value was:", repr(Decimal(10)))
    console output
  • >
    repr(10) == repr("10")
    Result:
    FalsePass Icon
  • Note that all of those repr results are legal Python syntax: Decimal('10') is the Python code to create that value.

  • >
    from decimal import Decimal

    Decimal('10') == eval(repr(Decimal('10')))
    Result:
    TruePass Icon
  • Here's a summary of when to use str vs. repr:

  • str returns an informal, and often imprecise, representation of a value. The str(...) may not clearly indicate the value's type, as we saw with integers vs. strings that happen to look like integers. We often use str when showing data to users. Most users don't care about whether a value is an integer or a Decimal.

  • repr is a more precise and exact representation of the value. It should indicate the data type as well as its value, and ideally it should be a valid Python expression. repr is intended for programmers, not users. We recommend using repr whenever you're debugging, generating logs, or generating reports intended for programmers.