Newbie: Check first two non-whitespace characters

MRAB python at mrabarnett.plus.com
Thu Dec 31 13:38:01 EST 2015


On 2015-12-31 18:18, otaksoftspamtrap at gmail.com wrote:
> I need to check a string over which I have no control for the first 2 non-white space characters (which should be '[{').
>
> The string would ideally be: '[{...' but could also be something like
> '  [  {  ....'.
>
> Best to use re and how? Something else?
>
I would use .split and then ''.join:

 >>> ''.join(' [ { ....'.split())
'[{....'

It might be faster if you provide a maximum for the number of splits:

 >>> ''.join(' [ { ....'.split(None, 1))
'[{ ....'




More information about the Python-list mailing list