str() vs repr() vs print() in Python

While learning Python, I came across three things that look very similar at first:

  • str()
  • repr()
  • print()

Initially, I thought all of them simply display output.
But after exploring deeper, I realized they actually serve different purposes.

The Core Idea

  • str() —> user-friendly representation
  • repr() —> developer/debugging representation
  • print() —> displays output on the screen

Let’s understand the things in detail

str() is used when we want output that is clean and readable for humans.

Here, Python gives us a simple readable version of the object.

But, repr() is different.

It tries to show the exact representation of the object – the way Python internally sees it.

Notice something interesting?

Quotes are visible here.

That’s because repr() is meant more for developers and debugging.

A Better Example

Now let’s take a string containing a newline character:

But using repr()

What’s Happening Here?

  • str() shows the readable version
  • repr() shows the raw/internal representation

That’s why n is visible inside repr().

So What Does print() Do?

print() is simply used to display output.

If you have noticed….Internally, print() usually uses str() to display objects.

Final Understanding

Function | Purpose
str() | Human-readable output
repr() | Developer/debugging output
print() | Displays output

Final Thoughts

At first, these functions looked almost identical to me.

But understanding the difference between readable output and internal representation made Python behavior much clearer — especially for debugging and understanding objects deeply.

Small concept, but very powerful.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

I Built Tautest: A Mutation Testing Workflow for AI-Written Tests

Related Posts