10 examples of 'str to int in python 3' in Python

Every line of 'str to int in python 3' 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
75def to_int(value):
76 return int(value)
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

58def to_int(input):
59 try:
60 return int(input)
61 except ValueError:
62 raise Exception ('Input is not an integer')
62def str_to_int(x):
63 try:
64 return int(str_to_float(x))
65 except: # noqa E722
66 return x
375def str_to_int(value, err_msg=None):
376 """
377 Converts str to int if str represents a valid int
378 :param value:
379 :type: str, int, or None
380 :param err_msg: A optional error message to use
381 :type: str
382 :returns:
383 :type: int or None
384 """
385 if is_str_unicode(value):
386 try:
387 a = int(value)
388 if value.isalpha(): # (ie. NaN or Infinity)
389 raise ValueError(err_msg)
390 else:
391 return a
392 except ValueError:
393 raise ValueError(err_msg)
394 elif isinstance(value, int):
395 return int(value)
396 elif value is None:
397 return None
398 else:
399 raise ValueError("str_to_int: Input value must be a string, int, or "
400 "None. Value was %s." % value)
226def _bytes_to_int(b):
227 return int(codecs.encode(b, "hex"), 16)
81def integer_from_Intel2(int_string):
82 # return first integer decoded from 2-byte-intel-format string
83 # jjk 04/25/96
84 return integer_from_Intel(int_string[:2])
40def is_int(s: str) -> bool:
41 try:
42 int(s)
43 return True
44 except ValueError:
45 return False
336def _convert_stringval(value):
337 """Converts a value to, hopefully, a more appropriate Python object."""
338 value = six.text_type(value)
339 try:
340 value = int(value)
341 except (ValueError, TypeError):
342 pass
343
344 return value
20def _to_int(value):
21 if value is None:
22 return 0
23 return int(value)
14def _force_int(a_string):
15 """Return the string as an int. If it can't be made into an int, return 0."""
16 try:
17 an_int = int(a_string)
18 except (ValueError, TypeError):
19 an_int = 0
20
21 return an_int

Related snippets