Skip to content

Instantly share code, notes, and snippets.

@RobiMez
Created July 11, 2022 20:46
Show Gist options
  • Save RobiMez/2a20e3e298d6685608cd579bbdcd79a2 to your computer and use it in GitHub Desktop.
Save RobiMez/2a20e3e298d6685608cd579bbdcd79a2 to your computer and use it in GitHub Desktop.
def padd_str(text, to_chars, push_r: bool = False, push_l: bool = False):
"""Padds the string given with spaces to a certain char count ( like setw() in cpp )"""
if len(text) < to_chars:
excess = to_chars - len(text)
if push_r: # we pad to the left of text
text = ' ' * excess + text
elif push_l: # we pad to the right of text
text = text + ' ' * excess
else: # we dont pad if no direction
print('No direction specified , returning normal')
assert len(text) == to_chars
return text
elif len(text) == to_chars:
return text
else:
print('Text larger than max chars.')
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment