Remove whitespace in Python using string.whitespace

后端 未结 5 628
夕颜
夕颜 2020-11-29 19:51

Python\'s string.whitespace is great:

>>> string.whitespace
\'\\t\\n\\x0b\\x0c\\r \'

How do I use this with a string without resor

5条回答
  •  广开言路
    2020-11-29 20:23

    There is a special-case shortcut for exactly this use case!

    If you call str.split without an argument, it splits on runs of whitespace instead of single characters. So:

    >>> ' '.join("Please \n don't \t hurt \x0b me.".split())
    "Please don't hurt me."
    

提交回复
热议问题