convert python string into integer without commas in them [duplicate]

  • Last Update :
  • Techknowledgy :

You can do this :

>>> string = '82,000,00' >>>
   int(price_result.replace(',', ''))

Checkout or depending on the Python version you are using and use the "replace()" function:

int_price = int(price_result.replace(',', ''))

This replaces all commas within the string and then casts it to an INT:

>>> price = "1,000,000"
>>> type(price)
<type 'str'>
   >>> int_price = int(price.replace(',',''))
   >>> type(int_price)
   <type 'int'>

If the last part is a fractional part, you could do something like this:

import re
r = re.compile(r '((?:\d{1,3},?)+)(,\d{2})')
m = r.match('82,000,00')
v =',', '') +',', '.')


import re

   ''.join(re.findall(r '\d+', '82,000,00'))

or another method will be,

int(filter(str.isdigit, '82,000,00'))

Suggestion : 2

To convert it to a decimal integer you need to use base 16: If you pass the D5CF string to the int () function without setting a base, it will throw a ValueError exception: In Python, you can convert a string to an integer using the int () function. , Convert string to integer in Python. In Python an strings can be converted into a integer using the built-in int () function. The int () function takes in any python data type and converts it into a integer.But use of the int () function is not the only way to do so. , Given a string of comma separated numbers, we want to convert that string to a Python list of integers The idea here is to split the string into tokens then convert each token to an integer. We can do that in a couple of ways. Let us see how… Same as before but using map… , 1 week ago The built-in int () function in Python converts a string to its corresponding integer value. This function can take any Python data type as its parameter and return the corresponding integer value, as long as it is definable. For example, A = "56" B = "40" print(int(A)+4) print(int(A)+int(B)) Output: 60 96. The int () function converts string A ...

 try: price = soup.find('span', {
    'id': 'actualprice'
 }) price_result = str(price.get_text()) print "Price: ", price_result except StandardError as e: price_result = "Error was {0}".format(e) print price_result

>>> string = '82,000,00' >>> int(price_result.replace(',', '')) 8200000
fave_phrase = "Hello world!"
#Hello world!is a string, enclosed in double quotation marks
fave_number = 7 #7 is an int # "7"
would not be an int but a string, despite it being a number.#This is because of the quotation marks surrounding it
#string version of the number 7 print("7") #check the data type with type() method print(type("7")) #output #7 #<class 'str'>
#convert string to int data type print(int("7")) #check the data type with type() method print(type(int("7"))) #output #7 #<class 'int'>

Suggestion : 3

It is possible to pass “long” integers (integers whose value exceeds the platform’s LONG_MAX) however no proper range checking is done — the most significant bits are silently truncated when the receiving field is too small to receive the value (actually, the semantics are inherited from downcasts in C — your mileage may vary).,The list of format units ends here; the string after the colon is used as the function name in error messages (the “associated value” of the exception that PyArg_ParseTuple() raises).,These formats allow accessing an object as a contiguous chunk of memory. You don’t have to provide raw storage for the returned unicode or bytes area.,If there is an error in the format string, the SystemError exception is set and NULL returned.

status = converter(object, address);
static PyObject *
   weakref_ref(PyObject * self, PyObject * args) {
      PyObject * object;
      PyObject * callback = NULL;
      PyObject * result = NULL;

      if (PyArg_UnpackTuple(args, "ref", 1, 2, & object, & callback)) {
         result = PyWeakref_NewRef(object, callback);
      return result;
PyArg_ParseTuple(args, "O|O:ref", & object, & callback)

Suggestion : 4

Merge two sorted arrays