[issue22129] Please add an equivalent to QString::simplified() to Python strings

R. David Murray report at bugs.python.org
Tue Aug 5 20:35:20 CEST 2014


R. David Murray added the comment:

Actually, we more or less already do have this function in the stdlib, (I'd guess it is for pretty much the same reason that QT has it), except ours can also truncate sensibly to a given width:

  >>> import textwrap
  >>> textwrap.shorten('  lots\t of\nwhitespace\r\n ', 99999)
  'lots of whitespace'
  >>> textwrap.shorten('  lots\t of\nwhitespace\r\n ', 15)
  'lots of [...]'


That said, if you want *just* the white-space-stripping, the ' '.join(s.strip().split()) expression is just as useful and (for what it is worth) faster.

----------
nosy: +r.david.murray

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22129>
_______________________________________


More information about the Python-bugs-list mailing list