How to find the first space?

Chris cwitts at
Tue Jun 10 08:47:14 CEST 2008

On Jun 9, 5:02 pm, Johny <pyt... at> wrote:
> How can I find the first space using regex?
> For example I have text
> Text=' This is a sample '
> The last space I can remove by
> Text=re.sub(r"\s(?!\w)",'',Text)
> but I do not know how to remove the first space.
> Can anyone help?
> Thanks
> L.

If it's leading spaces you're worried about you can use the .lstrip()
method, if you want to find the first space you can use .index(' ') to
get the position of it and do with what you want.

More information about the Python-list mailing list