4

What is the complete list of whitespace characters that print interprets as such?

I need to truly understand how the print statement works in Python. So far so weird, but this is one detail that I can't figure out:

In Python's reference manual chapter 6.6 it says:

... when the last character written to standard output is a whitespace character except ' ', ...

I know that the most common whitespace for this case is '\n', but I need to know the others.

4
  • May be you want this: import string;print repr(string.whitespace). Oct 18, 2013 at 9:17
  • I'd suggest trying to find a solution that doesn't require using that obscure part of the print statement's semantics. For instance, joining various substrings together into a single output string (that you have full control over). Or try from __future__ import print_function for a forwards compatible approach (which always includes its sep parameter between its arguments, even if they end in whitespace).
    – Blckknght
    Oct 18, 2013 at 9:24
  • @Blckknght I coudn't agree more. However, I have to reproduce the print statement's behaviour exactly. Not that I'd encourage anyone of depending on it.
    – Stefan
    Oct 18, 2013 at 9:27
  • I believe this is what was called the "soft-space" feature of print, which was removed in python3.
    – Bakuriu
    Oct 18, 2013 at 10:47

1 Answer 1

2

In CPython, it is probably all characters for which isspace() returns true, namely:

  • \f (form feed)
  • \n (linefeed)
  • \r (carriage return)
  • \t (horizontal tab)
  • \v (vertical tab)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.