Main Content
Backquote, backtic, str and repr in Python - conversion object to string Archive - Originally posted on "The Horse's Mouth" - 2012-07-05 07:46:12 - Graham Ellis
The backquote or backtic operator in Python is a pseudonym for the repr function.
If you have an object that you want to convert into a string (to manipulate it, store it, print it out), Python may call one of two standard method:
__str__ (also the str function) converts an object into a human-readable form - the sort of thing that your program's user might wish to see if you print an object out.
__repr__ (also the repr function) converts an object into a string which is what you as the programmer would wish to be fed back for debugging purposes.
The base object class from which every objects is derived these days includes a __repr__ method which returns a string containing the object type and id / memory address. There is no __str__ method in the base object, and the system will fallback to running __repr__ if you try to print out / convert to a string an object for which __str__ has not been overwritten.
The Backquote (back quote, backtic, back tic) operator runs repr (hard to find in an online search, as the backquote if often special syntax to or ignored by search engines!)
Sample program and output - [here] .
Some other articles
Y112 - Objects - Intermediate Nesting decorators Defining an object that is a modified standard type in Python with in Python - examples of use, and of defining your own context Object and Static methods - what is the difference; example in Python 3 Setting up and tearing down with the Python with keyword Deciding whether to use parameters, conditional statements or subclasses Spike solution, refactoring into encapsulated object methods - good design practise A good example of recursion - a real use in Python Changing what operators do on objects - a comparison across different programming languages Object factories in C++, Python, PHP and Perl Python base and inherited classes, test harness and unit testing - new examples Python Properties - how and why Really Simple Class and Inheritance example in Python Inheritance, Composition and Associated objects - when to use which - Python example This article Metaclasses (Python) and Metatables (Lua) Static variables in functions - and better ways using objects A demonstration of how many Python facilities work together A list of special method and attribute names in Python Python - some common questions answered in code examples Defining static methods in Python Should Python classes each be in their own file? The Light bulb moment when people see how Object Orientation works in real use Python decorators - your own, staticmethod and classmethod Mixins example in Python Multiple inheritance in Python - complete working example The Multiple Inheritance Conundrum, interfaces and mixins Methods that run on classes (static methods) in Python How do I set up a constant in Python? TypeError: super() argument 1 must be type, not classobj (Python) Python - fresh examples of all the fundamentals Calling base class constructors Equality, sameness and identity - Python Using a utility method to construct objects of different types - Python Python - formatting objects What are factory and singleton classes? __new__ v __init__ - python constructor alternatives? Practical polymorphism in action Pieces of Python Comparison of Object Oriented Philosophy - Python, Java, C++, Perl Think about your design even if you don't use full UML Class, static and unbound variables Overloading of operators on standard objects in Python Using a Python dictionary as a holder of object attributes Y108 - String Handling Prining a pound sign from Python AND running from the command line at the same time Python formatting update - including named completions Command line parameter handling in Python via the argparse module Python - comparison of old and new string formatters Identifying and clearing denial of service attacks on your Apache server Formatting options in Python Why are bus fares so high? Collections in Python - list tuple dict and string. Formatting output - why we need to, and first Python example This article Teaching dilemma - old tricks and techniques, or recent enhancements? Python string formatting - the move from % to str.format Formatting output in Python through str.format Matching a license plate or product code - Regular Expressions Matching to a string - what if it matches in many possible ways? Python - splitting and joining strings Formatted Printing in Python Running operating system commands from your Python program Regular Expressions in Python Flexible search and replace in Python Pound Sign in Python Program Strings as collections in Python Python Regular Expressions Underlining in Perl and Python - the x and * operator in use Python - formatting objects Regular Express Primer Python - two different splits String duplication - x in Perl, * in Python and Ruby Splitting Pythons in Bradford Matching within multiline strings, and ignoring case in regular expressions Pieces of Python Breaking bread The fencepost problem Python printf Splitting the difference The backtick operator in Python and Perl