Linked Questions
337 questions linked to/from What is the difference between __str__ and __repr__?
97
votes
8
answers
100k
views
What is the purpose of __str__ and __repr__? [duplicate]
I really don't understand where are __str__ and __repr__ used in Python. I mean, I get that __str__ returns the string representation of an object. But why would I need that? In what use case scenario?...
38
votes
8
answers
99k
views
What is doing __str__ function in Django? [duplicate]
I'm reading and trying to understand django documentation so I have a logical question.
There is my models.py file:
from django.db import models
class Blog(models.Model):
name = models....
34
votes
1
answer
50k
views
str() vs repr() functions in python 2.7.5 [duplicate]
what is the difference between str() and repr() functions in python 2.7.5?
Explanation on python.org:
The str() function is meant to return representations of values which are fairly human-readable,...
8
votes
2
answers
68k
views
working of \n in python [duplicate]
I am currently taking a course in python. When talking about escape sequences they told about "\n" is used for printing strings in new line. But when it is used in the following ways why I am getting ...
11
votes
1
answer
10k
views
The difference between __str__ and __repr__? [duplicate]
I write this code:
class Item:
def __init__(self, name):
self._name = name;
def __str__(self):
return "Item: %s" % self._name
When I run
print((Item("Car"),))
the output is
(...
5
votes
4
answers
1k
views
Printing a list of objects [duplicate]
I am a Python newbie. I have this small problem. I want to print a list of objects but all it prints is some weird internal representation of object. I have even defined __str__ method but still I am ...
4
votes
2
answers
7k
views
String representation of an object in Python [duplicate]
So in the book(Problem Solving with Algorithms and Data Structres) that I'm currently using as a reference, this is how a graph is implemented. What I don't understand here is how exactly the __str__ ...
5
votes
1
answer
10k
views
Adding a new line character to a variable in python [duplicate]
I have the below function to get the below output
22
4444
666666
Instead i'm getting
'22\n4444\n666666\n88888888\n'
Any ideas where im going wrong?
def EvenLadder(n):
...: solution = ''
...
5
votes
2
answers
4k
views
Python - concatenate a string to include a single backslash [duplicate]
In Python 2.6, I need to create a string by concatenating INTRANET\ and a userid such as jDoe to obtain a string INTRANET\jDoe. This will string will be a part of a SQL query. I have tried this a ...
8
votes
1
answer
4k
views
Angle brackets in Python [duplicate]
I want to craft packets using scapy. When looking through the IP() class members I came across the following code idiom:
'fieldtype': {
'frag': <Field (IP,IPerror).frag>,
'src': <...
1
vote
1
answer
2k
views
what is the difference between __repr__ and __str__? [duplicate]
class A():
def __init__(self,x):
self.x = x
def __repr__(self):
return self.x
For example, if I have a statement like A("one"), the output is one.
Therefore, can I assume ...
0
votes
1
answer
3k
views
__str__ function not returning a string, but instead the instance? [duplicate]
I've looked through many different forum posts and have tried a bunch of different techniques, but I have not figured out how to get my str function to return a string ' ' rather than having no ...
0
votes
2
answers
2k
views
Python prints two backslash instead of one [duplicate]
My goal is to print a backslash in Python3. My input is
links22 = ['1',"n","nkf"]
treee = ['<img src={} \>'.format(i) for i in links22]
print(treee)
The output that I get is:
['<img src=1 \...
3
votes
2
answers
727
views
Difference between variable and print variable [duplicate]
I have the following code (Assuming I am typing in IDLE line by line)
# -*- coding: utf-8 -*-
s = u"My Currency is - £"
s
print s
for - s - I'm getting an output - u'My Currency is - \xa3'
for - ...
4
votes
1
answer
259
views
Last decimal digit precision changes in different call of same generator function [python] [duplicate]
I created this generator function:
def myRange(start,stop,step):
r = start
while r < stop:
yield r
r += step
and I use it in two different ways. 1st:
for x in myRange(0,1,...