How to get rid of space in a string in python? -
i trying take string in list of strings , remove white space while separating non-space comma. example have string:
' xx xx x xxxxxx xx xxxxxxx xxxxx' and need return ['xx', 'xx', 'x', 'xxxxxx', 'xx', 'xxxxxxx', 'xxxxx']. suggestions?
note: amount of space between each segment varies.
use str.split(), exactly need already, splitting on arbitrary-width whitespace:
>>> example = ' xx xx x xxxxxx xx xxxxxxxx xxxxx' >>> example.split() ['xx', 'xx', 'x', 'xxxxxx', 'xx', 'xxxxxxxx', 'xxxxx'] note leading whitespace has been removed well.
quoting documentation:
if sep not specified or
none, different splitting algorithm applied: runs of consecutive whitespace regarded single separator, , result contain no empty strings @ start or end if string has leading or trailing whitespace. consequently, splitting empty string or string consisting of whitespacenoneseparator returns[].
Comments
Post a Comment