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
\s matches a whitespace character. \S matches a non-whitespace character.
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.
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]
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
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!'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With