0

Why do python shell gives different output

Example: >>>r'\a' '\\a' Print(r'\a') '\a' >>>'\a' '\X07' Print('\a') []

17th Apr 2020, 8:00 AM
poonam panchal
poonam panchal - avatar
1 Answer
+ 1
It's probably this: When you type a value on a line in the interpreter and hit enter, it calls the __repr__ method belonging to that value. But, when you print the value, print automatically calls __str__ on it. You can see this in action: class Test: def __repr__(self): return '__repr__ called' def __str__(self): return '__str__ called' >>> t = Test() >>> t '__repr __ called' >>> print(t) '__str__ called' also '\a' is a special character like '\n': ord('\a') => 7 It differs from raw string r'\a' which treats '\' as a normal backslash character that doesn't do anything. In normal strings you should escape the backslash like this: '\\a'
19th Apr 2020, 12:25 PM
Zuke
Zuke - avatar