10 examples of 'how to convert string to integer in python' in Python

Every line of 'how to convert string to integer 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
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
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

66def cast_to_integer(text_value):
67 return int(text_value)
75def to_int(value):
76 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
59def integer(value):
60 """Handles conversions for string respresentations of binary, octal and hex."""
61 if isinstance(value, basestring):
62 return int(value, 0)
63 else:
64 return int(value)
6def convert_string_to_float_int(string):
7
8 if isinstance(string, six.string_types):
9 try:
10 # evaluate the string to float, int or bool
11 value = literal_eval(string)
12 except Exception:
13 value = string
14 else:
15 value = string
16
17 # check if the the value is float or int
18 if isinstance(value, float):
19 if value.is_integer():
20 return int(value)
21 return value
22 return value
475def read_int(inputstring):
476 if isinstance(inputstring, tuple):
477 inputstring = inputstring[0]
478 return int(inputstring)
58def to_int(input):
59 try:
60 return int(input)
61 except ValueError:
62 raise Exception ('Input is not an integer')
86def charToInt(char):
87 if char =="":
88 return 0
89 else:
90 return int(char)
20def _to_int(value):
21 if value is None:
22 return 0
23 return int(value)

Related snippets