I just ran into what seemed to me to be bizarre string formatting behavior in python. It turned out to be caused by carriage return characters ('\r') I didn't know were there. Here's an example:
>>> text = 'hello\r'
>>> '(SUBJECT "%s")' % (text)
'(SUBJECT "hello\r")'
>>> print '(SUBJECT "%s")' % (text)
")UBJECT "hello
I tried the same thing in C (on a couple machines) as a sanity check.
#include <stdio.h>
int main()
{
char *text = "hello\r";
printf("(SUBJECT \"%s\")\n", text);
return 0;
}
Output:
% ./a.out
")UBJECT "hello
Is this desired behavior? If so, can someone explain what is going on?
It (\r) is a carridge return without a linefeed so the cursor moves back to the start of the current line without moving onto a new line and therefore overwriting what is already displayed.
The behaviour depends on your console and whether it interprets CR and LF as individual operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With