Difference Between repr(), str(), and print() function in Python.

Difference Between repr(), str(), and print() function in Python.

·

2 min read

Certainly! In Python, repr(), str(), and print() function serve different purposes, but they are related in that they deal with how objects are represented or converted into strings.

Let’s see deep inside how each function deal with how object are represented or converted into strings.

1)repr(): In Python repr() is a built-in function that return the printable representation of the specified object as a string.

Or

Returns a printable representation of the object by converting that object to a string.

Syntax:-

Example:-

In the above program, we have assigned the value ‘Hello World’ to txt. The repr() function return the value as “ ’Hello World’ ”, ‘Hello World’ inside double-qoutes. The same goes for the other variables and their repr() return values.

2)str(): In Python str() is a built-in function in programming language that is used to convert the specified value into a string datatype.

Syntax:-

Example:-

In the above program, we assign an integer value to a variable and convert that integer variable to the string variable and print it in Python.

3)print(): This function is used to display text on the console. It converts objects to strings using their str() method if available, or their repr() method if str() is not defined.

Syntax:-

Example:-

A small comparison between str(), repr(), print() function in Python.

Function/PurposeOutputUsage
repr()Returns unambiguous representation of an object.Unambiguous, developer-focused.Mainly for debugging and logging.
str()Returns human-readable representationReadable, user-focused.When output is meant for end-users.
print()Outputs text or data to the console.Converts objects to strings using str() or repr().Displaying information on to the user.

"Here the word Unambiguous in programming means clear and not open to interpretation.

Thanks for reading ;)…

Follow me on my instagram handle:-

https://www.instagram.com/yashpale_?igsh=MmVlMjlkMTBhMg==