10 examples of 'how to extract digits from a number in python' in Python

Every line of 'how to extract digits from a number in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
84def _check_digit(number):
85 """Calculate a single check digit on the provided part of the number."""
86 weights = (13, 15, 12, 5, 4, 17, 9, 21, 3, 7, 1)
87 s = sum(w * (int(n) if n.isdigit() else alphabet.find(n) + 1)
88 for w, n in zip(weights, number))
89 return 'MQWERTYUIOPASDFGHJKLBZX'[s % 23]
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

32def digits(s):
33 if not s:
34 return ''
35 if type(s) is int:
36 return s
37 return re.sub('[^0-9]', '', s)
45def sum_digits(num):
46 return sum(map(int, list(str(num))))
156def get_numbers(num):
157 card_and_port = str(num)
158 card_and_port = re.split('/|-',card_and_port)
159 card = card_and_port[0]
160 fromPort = card_and_port[1]
161 toPort = fromPort if len(card_and_port) <= 2 else card_and_port[2]
162 return card, fromPort, toPort
71def calc_check_digit(number):
72 """Calculate the check digit. The number passed should have the check
73 digit included."""
74 checksum = (1 - 2 * int(number[:-1], 13)) % 11
75 return 'X' if checksum == 10 else str(checksum)
54def calc_check_digit(number):
55 """Calculate the check digit for personal codes. The number passed
56 should not have the check digit included."""
57 # note that this algorithm has not been confirmed by an independent source
58 weights = (2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9)
59 check = sum(w * int(n) for w, n in zip(weights, number)) % 11
60 return '1' if check == 10 else str(check)
110def calc_check_digit(number):
111 """Calculate the extra digit that should be appended to the number to
112 make it a valid number."""
113 return str(_multiplication_table[checksum(str(number) + '0')].index(0))
9def find_nth_digit(n):
10 len = 1
11 count = 9
12 start = 1
13 while n > len * count:
14 n -= len * count
15 len += 1
16 count *= 10
17 start *= 10
18 start += (n - 1) / len
19 s = str(start)
20 return int(s[(n - 1) % len])
20def ParseNumber(number):
21 if number.startswith('0x'):
22 return int(number[2:], 16)
23 else:
24 return int(number)
48def calc_check_digit(number):
49 """Calculate the check digit. The number passed should not have the
50 check digit included."""
51 weights = (21, 19, 17, 13, 11, 9, 7, 3, 1)
52 return str(sum(w * int(n) for w, n in zip(weights, number)) % 10)

Related snippets