0% found this document useful (0 votes)
44 views23 pages

Python String Representation Methods

- str() produces a human-friendly string representation of an object by calling the object's __str__() method - repr() produces an unambiguous string by calling the object's __repr__() method, including type information for developers - __format__() is called by str.format() and allows custom formatting of string representations

Uploaded by

SalmaanCadeXaaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views23 pages

Python String Representation Methods

- str() produces a human-friendly string representation of an object by calling the object's __str__() method - repr() produces an unambiguous string by calling the object's __repr__() method, including type information for developers - __format__() is called by str.format() and allows custom formatting of string representations

Uploaded by

SalmaanCadeXaaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Strings and Representations

Austin Bingham Robert Smallshire


@austin_bingham @robsmallshire
austin@[Link] rob@[Link]
Presenter
str() repr()
functions for making
string representations
from Python objects
which call the methods

__str__() __repr__()
The built-in function

repr()
produces an unambiguous string
representation of an object.

Point2D(x=42, y=69)
repr()
! Exactness is more important than human-friendliness

! Suited for debugging

! Includes identifying information

! Generally best for logging


repr() is for deve
lopers

The result of repr()


should generally contain
more information than
the result of str()

fo r c l i e nts
str() is
As a rule, you should
always write a repr()
for your classes

r e p r ( )
d ef a u l t
The h e l p f u l
v e r y
is not
The built-in function

str()
produces a readable, human-friendly
representation of an object

Remember - str r a m m e r
() Not p r o g
is also the strin o r i e n t e d
g
constructor
By default, str()
simply calls repr()
But repr() does not call str()
repr() is used when showing
elements of a collection
The special method

__format__()
invoked by [Link]()
>>> obj = MyClass()
>>> "{:XYZ}".format(obj)

class MyClass:

def __format__(self, f):


# Return a string representation of self
# formatted according to the formatting
# options in format specifier f
# ...
>>> obj = MyClass()
>>> "{:XYZ}".format(obj)

Replacement fields
{field_name:format_spec}

Optional format specification


class MyClass: after colon
def __format__(self, f):
# Return a string representation of self
# formatted according to the formatting
# options in format specifier f
# ...
The standard library module

reprlib
supports alternative
implementations of repr()

• limits otherwise
excessive string length
• useful for large
collections
The standard library function

[Link]()
is a drop-in replacement for repr()
The standard library class

[Link]
• implements the main functionality of reprlib
• supports customisation through subclassing
• [Link]
[Link]
r
an instance use
d
by Python and
debuggers
The built-in functions

ascii()
ord() chr()
The built-in function

ascii()
replaces non-ASCII characters with escape sequences
The built-in function

chr()
Converts an integer Unicode
codepoint to a single character string
The built-in function

ord()
converts a single character to its
integer Unicode codepoint
Duck Tails
Bigger Isn't Always Better
Duck Tails
Sometimes the
repr() of an object will
be shorter than
its str()
Strings and Representations
• for humans!
print(obj)
an object • fallback to repr()

str(obj) __str__(self)

repr(obj) __repr__(self)

"{:f}".format(obj)
• unambiguous
__format__(self, f)
• precise
• include type
• for developers
• fallback to str(
)
import reprlib
[Link](obj)
s = ascii(string)
from reprlib import Repr
i = ord(c)
class MyRepr(Repr):
# ... c = chr(i)

You might also like