you could get substring text[:24], remove spaces on its right end rstrip() and get length of both strings and substract them.
text[:24]
rstrip()
spaces += len(table_heading_raw[:raw_index]) - len(table_heading_raw[:raw_index].rstrip()) - 1
from furas answer