Last active
December 14, 2017 13:31
-
-
Save shoark7/ef8797fedb0e836f4a0778e4a4d552a6 to your computer and use it in GitHub Desktop.
Implement str.zfill method as function
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 is_integer(n): | |
| """Return True if given n is integer. | |
| Difference with isinstance(n, int) is that | |
| this function returns True for string instance '123'. | |
| """ | |
| try: | |
| int(n) | |
| except: | |
| return False | |
| return True | |
| def zero_fill(number, zeros=4): | |
| """Fill up zeros for the number if number is short""" | |
| if not is_integer(number): | |
| raise ValueError("Only Integer is accepted") | |
| number = str(number) | |
| if len(number) >= zeroes: | |
| return | |
| return '0' * (zeroes - len(number)) + number | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oh, you made a function to figure out whether the input is integer or not. Great Job!