views:

119

answers:

6

What's the shortest way to do this in Python?

string = "   xyz"

must return index = 3

+1  A: 
>>> next(i for i, j in enumerate('   xyz') if j.strip())
3

or

>>> next(i for i, j in enumerate('   xyz') if j not in string.whitespace)
3

in versions of Python < 2.5 you'll have to do:

(...).next()
SilentGhost
next is not available in 2.5, but from 2.6 on, right?
Pablo
@Pablo: that's why I showed how to do this with `.next()`
SilentGhost
`blah.strip()` and `blah.isspace()` work OK with Unicode; string.whitespace is frozen in the last century.
John Machin
@John: says who? I see `string.whitespace` as the second most efficient approach after the accepted one.
SilentGhost
@SilentGhost: Re-read my comment. I'm talking about working with Unicode; no mention of efficiency.
John Machin
+11  A: 
>>> s = "   xyz"
>>> len(s) - len(s.lstrip())
3
Frank
nice answer ^^;
Pablo
If s is long and the whitespace prefix is short, other solutions (ones that don't make a temp almost-copy of s, get its length, and then throw the temp object away) may be preferable.
John Machin
A: 
>>> string = "   xyz"
>>> next(idx for idx, chr in enumerate(string) if not chr.isspace())
3
Adrien Plisson
A: 
>>> string = "   xyz"
>>> map(str.isspace,string).index(False)
3
ghostdog74
+1  A: 

Looks like the "regexes can do anything" brigade have taken the day off, so I'll fill in:

>>> tests = [u'foo', u' foo', u'\xA0foo']
>>> import re
>>> for test in tests:
...     print len(re.match(r"\s*", test, re.UNICODE).group(0))
...
0
1
1
>>>

FWIW: time taken is O(the_answer), not O(len(input_string))

John Machin
A: 
import re
def prefix_length(s):
   m = re.match('(\s+)', s)
   if m:
      return len(m.group(0))
   return 0
D.Shawley
"""Make sure your code "does nothing" gracefully.""" -- attributed to Jon Bentley IIRC.
John Machin
Forgive me my ignorance, but who is him?
Pablo
Ignorance is forgivable; unwillingness to use a search engine is another matter ;-) http://en.wikipedia.org/wiki/Jon_Bentley
John Machin
@JohnMachin - D'oh... good point about `+` instead of `*`. My thinking cap wasn't fully on this morning.
D.Shawley
Also you have redundant parentheses.
John Machin