How can I easily check if a line starts with 4N spaces?

How can I easily check if a line starts with spaces 4*N, where Nis a positive integer?

My current code is:

def StartsWith4Nspaces(string):
    count = 0
    for c in string:
        if c == ' ':
            count += 1
        else:
            break
    return count > 0 and count % 4 == 0

Is there a more pythonic way to spell this?

I really hope for one statement (although nothing cleaner than the above would be great).

Thank.

+4
source share
9 answers

You can simply check it like this:

my_string[:4*N] == ' ' * 4*N

You can also convert this check to lambda:

check = lambda my_string, N: my_string[:4*N] == ' ' * 4*N

and name it like:

check('  asdas', 2) # -> True
check('  asdas', 3) # -> False

Or if you want to hard install Nfor any reason ( N = 3):

check = lambda my_string: my_string[:12] == ' ' * 12

EDIT: If a symbol does not require a 4Nth + 1 symbol, you can include it in your lambda:

check_strict = lambda my_string, N: my_string[:4*N] == ' ' * 4*N and my_string[4*N + 1] != ' '

check_strict = lambda my_string: my_string[:12] == ' ' * 12 and my_string[13] != ' '
+4

lstrip , :

s = string.lstrip()
return ((len(string) - len(s)) % 4 == 0 and (len(string) - len(s) != 0)

( , s.)

+2

:

>>> re.match('(?: {4})*(?! )', '')
<_sre.SRE_Match object at 0x7fef988e4780>
>>> re.match('(?: {4})*(?! )', '  ')
>>> re.match('(?: {4})*(?! )', '    ')
<_sre.SRE_Match object at 0x7fef988e4718>
>>> re.match('(?: {4})*(?! )', 'foo')
<_sre.SRE_Match object at 0x7fef988e4780>
>>> re.match('(?: {4})*(?! )', '  foo')
>>> re.match('(?: {4})*(?! )', '    foo')
<_sre.SRE_Match object at 0x7fef988e4718>
>>> re.match('(?: {4})*(?! )', '      foo')
>>> re.match('(?: {4})*(?! )', '        foo')
<_sre.SRE_Match object at 0x7fef988e4780>

, N 0 , . , bool(), , bool. * + N 0.

+2
def startsWith4Nspaces(s):
    if not s: return False

    numLeadingSpaces = len(s) - len(s.lstrip(' '))
    if not numLeadingSpaces: return False
    if numLeadingSpaces%4: return False
    return True
+1

. , "" , . . :

mystring[:4] == '    '

startswith :

mystring.startswith('    ')

, 5 , True. 4, .

, ' '*N, N - , .

+1

def StartsWith4Nspaces(s):
    diff = len(s) - len(s.lstrip(' '))
    return ((diff > 0) and not (diff%4))

print(StartsWith4Nspaces('abc'))
print(StartsWith4Nspaces(' ' * 1 + 'abc'))
print(StartsWith4Nspaces(' ' * 4 + 'abc'))
print(StartsWith4Nspaces(' ' * 6 + 'abc'))
print(StartsWith4Nspaces(' ' * 8 + 'abc'))

False
False
True
False
True

.

+1

, N - , - , ;

import re    
def starts_with_four_n_spaces(eval_string):
    return re.search(r'^(?:\s{4})+(?!\s).*$', eval_string) is not None

;

>>> starts_with_four_n_spaces('  foo')
False
>>> starts_with_four_n_spaces('        foo')
True

^(?:\s{4})+(?!\s).*$

  • ^(?:\s{4})+ Match a few spaces
  • (?!\s) argue that this is not followed by another space;
  • .*$ match everything to the end of the line.
0
source

Another regular call:

re.search('[^ ]', string).start() % 4 == 0

Finds the index of the first non-spatial character and modulos wrt 4.

Or the same approach with a list:

next(i for i, c in enumerate(string) if c != ' ') % 4 == 0

0
source

You can slicestring and use the built-in method all()to check the chopped stringwhat you need in the following:

st = '    testString'
N = 1
print all(x == ' ' for x in st[:4*N])

displays

True
0
source

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


All Articles