Basically, if I have a line of text which starts with indention, what\'s the best way to grab that indention and put it into a variable in Python? For example, if the line i
This can also be done with str.isspace and itertools.takewhile instead of regex.
import itertools
tests=['\t\tthis line has two tabs of indention',
' this line has four spaces of indention']
def indention(astr):
# Using itertools.takewhile is efficient -- the looping stops immediately after the first
# non-space character.
return ''.join(itertools.takewhile(str.isspace,astr))
for test_string in tests:
print(indention(test_string))