Please help on print string that contains 'tab' and 'newline'

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jan 28 20:34:28 EST 2018


Jason, your Python output and the C output are not the same.

Also, it looks like your email client is adding formatting codes to the 
email, or something. Please look at your post here:

https://mail.python.org/pipermail/python-list/2018-January/730384.html

Do you notice the extra asterisks added? I think they are added by your 
email program. Please do not use any formatting, they make the output 
confusing and make it difficult to understand what you are doing.

In Python, the output of print(repr(message)) looks like this:

does not exist\r\n\tat com.

(removing the asterisks at the start and end, which I believe are 
formatting added by to your email). That is, spelling it out character by 
character:

    DE OH EE ES space EN OH TE space EE EX I ES TE backslash
    AR backslash EN backslash TE AY TE space CE OH EM dot

That is what I expected. So if you print(message) without the repr, you 
should get exactly this:

does not exist
	at com.



Here is the input and output of my Python session:

py> s = 'does not exist\r\n\tat com.'
py> print(s)
does not exist
        at com.



That looks like exactly what you are expecting. If you are getting 
something different, please COPY AND PASTE as PLAIN TEXT (no formatting, 
no bold, no italics) the exact output from your Python session. And tell 
us what IDE you are using, or which interpreter. Are you using IDLE or 
PyCharm or Spyder or something else?

I repeat: do not add bold, or italics, do not use formatting of any kind. 
If you don't know how to turn the formatting off in your email program, 
you will need to find out.


You also print the individual characters of message from C:

for ch in message:
  printf("%d %c",ch, chr(ch))

and that outputs something completely different: the messages are not the 
same in your C code and your Python code.

Your Python code message is:

does not exist\r\n\tat com.


but your C code message appears to be:

not exist\r\n\tat com

(missing word "does" and final dot).

I do not understand why you are showing us C code. This is a Python 
forum, and you are having trouble printing from Python -- C is irrelevant.

I think you should just call print(message). If that doesn't do what you 
want, you need to show us what it actually does.



-- 
Steve




More information about the Python-list mailing list