Linked Questions

97 votes
8 answers
100k views

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?...
Daniel's user avatar
  • 981
38 votes
8 answers
99k views

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....
Tornike Gomareli's user avatar
34 votes
1 answer
50k views

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,...
AnV's user avatar
  • 2,834
8 votes
2 answers
68k views

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 ...
Srividya's user avatar
  • 119
11 votes
1 answer
10k views

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 (...
BlackMamba's user avatar
  • 10.3k
5 votes
4 answers
1k views

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 ...
one-zero-zero-one's user avatar
4 votes
2 answers
7k views

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__ ...
prithajnath's user avatar
  • 2,135
5 votes
1 answer
10k views

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 = '' ...
Jayaram's user avatar
  • 6,626
5 votes
2 answers
4k views

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 ...
sunny's user avatar
  • 141
8 votes
1 answer
4k views

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': <...
zan's user avatar
  • 355
1 vote
1 answer
2k views

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 ...
BigBro666's user avatar
0 votes
1 answer
3k views

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 ...
CoopStad's user avatar
0 votes
2 answers
2k views

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 \...
John Karimov's user avatar
3 votes
2 answers
727 views

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 - ...
Kartheek Palepu's user avatar
4 votes
1 answer
259 views

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,...
Alvin's user avatar
  • 383

15 30 50 per page
1
2 3 4 5
23