How to find index of first character without spaces in string in python?

Scenario:

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

In this case, the index "H" is equal to "3". But I need a more general method, so does any string variable "a" need to know the index of the first character?

Alternative scenario:

 >>> a='\tHello world' index = 1 
+4
source share
5 answers

If you mean the first character without spaces, I would use something like this ...

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

Another fun:

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

But I bet that the first version works faster, since this exact loop does it, only in C. Of course, he needs to build a new line when it is done, but it is essentially free.


For completeness, if a line is empty or consists of completely spaces, both of them will return len(a) (which is not true 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 
+4
source

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 cases when the string is empty or contains only 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!' 
+2
source

You can also try:

 a = ' Hello world' a.index(a.lstrip()[0]) => 3 

It will work as long as the string contains at least one non-spatial character. We can be a little more careful and check this out earlier:

 a = ' ' -1 if not a or a.isspace() else a.index(a.lstrip()[0]) => -1 
+2
source

Another method, just for fun ... Using a special function!

 >>> def first_non_space_index(s): for idx, c in enumerate(s): if not c.isspace(): return idx >>> a = ' Hello world' >>> first_non_space_index(a) 3 
+1
source

Following msilson's answer, you can use lstrip to remove any characters you want -

 unwanted = ': !@ #$%^&*()_+ \t\n' a= ' _Hello world' res = len(a) - len(a.lstrip(unwanted)) 
0
source

Source: https://habr.com/ru/post/1486030/


All Articles