Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Last active September 15, 2024 08:49
Show Gist options
  • Save telekosmos/66eeb6c8494f508a41241e05c3484a36 to your computer and use it in GitHub Desktop.
Save telekosmos/66eeb6c8494f508a41241e05c3484a36 to your computer and use it in GitHub Desktop.
python slicing example
def format_number(n):
"""
Format a number by inserting commas as a thousands separator.
"""
str_number = str(n)
if len(str_number) <= 3:
return str_number
last3 = str_number[-3:]
rest = str_number[:-3]
return format_number(int(rest)) + ',' + last3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment