Last active
September 15, 2024 08:49
-
-
Save telekosmos/66eeb6c8494f508a41241e05c3484a36 to your computer and use it in GitHub Desktop.
python slicing example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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