Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the index of the first non-whitespace character in a string in python?

Tags:

python

string

Scenario:

>>> a='   Hello world'
index = 3

In this case the "H" index is '3'. But I need a more general method such that for any string variable 'a' takes I need to know the index of the first character?

Alternative scenario:

>>> a='\tHello world'
index = 1
like image 360
Kaushik Avatar asked Jun 13 '13 14:06

Kaushik


People also ask

What is non whitespace character in Python?

\s matches a whitespace character. \S matches a non-whitespace character.

How do you find the index value of a character in python?

To find the index of a list element in Python, use the built-in index() method. To find the index of a character in a string, use the index() method on the string. This is the quick answer.

How do you find the start and end index of a substring in Python?

index() Parameters The index() method takes three parameters: sub - substring to be searched in the string str . start and end(optional) - substring is searched within str[start:end]


2 Answers

If you mean the first non-whitespace character, I'd use something like this ...

>>> a='   Hello world'
>>> len(a) - len(a.lstrip())
3

Another one which is a little fun:

>>> sum(1 for _ in itertools.takewhile(str.isspace,a))
3

But I'm willing to bet that the first version is faster as it does essentially this exact loop, only in C -- Of course, it needs to construct a new string when it's done, but that's essentially free.


For completeness, if the string is empty or composed of entirely whitespace, both of these will return len(a) (which is invalid if you try to index with it...)

>>> a = "foobar"
>>> a[len(a)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
like image 196
mgilson Avatar answered Oct 11 '22 17:10

mgilson


Using regex:

>>> import re
>>> a='   Hello world'
>>> re.search(r'\S',a).start()
3
>>> a='\tHello world'
>>> re.search(r'\S',a).start()
1
>>>

Function to handle the cases when the string is empty or contains only white spaces:

>>> def func(strs):
...     match = re.search(r'\S',strs)
...     if match:
...         return match.start()
...     else:
...         return 'No character found!'
...     
>>> func('\t\tfoo')
2
>>> func('   foo')
3
>>> func('     ')
'No character found!'
>>> func('')
'No character found!'
like image 24
Ashwini Chaudhary Avatar answered Oct 11 '22 17:10

Ashwini Chaudhary