Menu

Post image 1
Post image 2
Post image 3
Post image 4
Post image 5
Post image 6
Post image 7
1 / 7
0

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

DEV Community·Samiksha Srivastav·22 days ago
#g9OgoZRC
#final#python#programming#webdev#repr#output
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?…

Continue reading — create a free account

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

Read More