Menu

1 / 7

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

DEV Community·Samiksha Srivastav·6 months ago
#g9OgoZRC
#final#python#programming#webdev#repr
Reading 0:00
15s threshold

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…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.