This question already has answers here:
How to extract a floating number from a string [duplicate]
(7 answers)
Closed 8 years ago.
I wander if there is a simple way to convert a string to a number, knowing that the string begins with numbers but can contain non numerical characters.
For example: my_str = "36.12minuts"
I remember a function in Visual Basic that does the conversion directly :
my_str = "36.12minuts"
val(my_str) => 36.12
How about Python?
def digitsndots(text):
if text in ["1","2","3","4","5","6","7","8","9","0","."]:
return True
else:
return False
num = float(filter(digitsndots, "36.12minuts"))
print num
When using this make sure your string does not have digits in between like "1.a.34.c" (courtesy of #Taha)
Related
This question already has answers here:
Python - Split integer into individual digits (undetermined amount of digits)
(4 answers)
Closed 1 year ago.
I cannot describe clearly because English is not my native language
If I input like this
a = 4252
Then I want to take each number component, it should be like this
a1=4; a2=2; a3=5; a4=2
How to do that in python?
If you know it has a similar question in stackoverflow, give me a link
Convert the integer to a string for easy iterability then get the int value of every character like this: a1,a2,a3,a4 = [int(elem) for elem in str(a)]
This question already has answers here:
How to convert an integer to a string in any base?
(35 answers)
Closed 2 years ago.
I want to take this string '01101011' as an 0b01101011 integer in Python.I couldn't find any method to do that.I tried:
a=a+"0b"
a=int(a)
But is has no use.Can you help me guys?
String to bitstring:
Convert the string to integer then to binarystring.
>>> a_str = '101011'
>>> a_bit_str = bin(int('101011',2))
>>> a_bit_str
'0b101011'
String to Integer:
If you just want to convert string to integer
>>> int(a_str,2)
43
This question already has answers here:
How to check if a specific digit is in an integer
(5 answers)
Closed 2 years ago.
So I basically want to know whether there is any syntax that can recognize if the integer entered by the user contains some specified digit
For example: 23 has the digit 3.
Convert the digit to a string, then check if the digit is in the string.
For example, I want to check if 9 is in the number.
x = str(50192301)
if '9' in x:
return True
This question already has answers here:
Python: Converting string into decimal number
(8 answers)
Closed 4 years ago.
I have a number 3.8148116e-09
How do I convert it to a real number without the - ?
Thanks.
You can try:
>>> a = "3.8148116e-09"
>>> number = float(a)
>>> print "{:1.16f}".format(number)
0.0000000038148116
The first line parses the string as a number. If you need to print the number or format it for another reason, you can use string#format.
This question already has answers here:
How can I convert a string with dot and comma into a float in Python
(9 answers)
Closed 4 years ago.
my question is simple.
I got my string :
a = '0,0127'
I want to convert it to a number but when i compile
float(a)
i got the following message error :
ValueError: could not convert string to float: '0,0127'
Is there another way to convert it to a number ?
Using str.replace
Ex:
a = '0,0127'
print(float(a.replace(",", ".")))
Output:
0.0127
The reason this isn't working is because the decimal type only recognizes periods (.) for the decimal delimiter as this is what is common in, e.g., english. You could manually change the string or do
a = a.replace(",", ".")
float(a)
Which should work.