How to convert String into Float? [duplicate] - python

This question already has answers here:
How do I parse a string to a float or int?
(32 answers)
Closed 6 years ago.
I'm required to covert the variable:
pi_string = "3.1415926"
into a float. Here's what I'm dealing with:

Your line should be pi_float = float(pi_string)
float(pi_string) is a float value, you can not assign to it, because it is not a variable.

The method float() will do this for you if pi_string = "3.1415926".
>>>pi_float = float(pi_string)
>>>type(pi_float)
<class 'float'>

Related

How to parse string '5e-04' to numerical 0.0005? [duplicate]

This question already has answers here:
Convert string (in scientific notation) to float
(4 answers)
Closed 9 months ago.
How can I parse a character string that represents a scientific number into a numeric number?
>>> float('5e-04')
0.0005

how to convert the bytes to int64 in the python [duplicate]

This question already has answers here:
Convert bytes to int?
(7 answers)
Closed 2 years ago.
My question is:
how can we convert the bytes to int64 in python
in C# we could use BitConverter.ToInt64()for transfer the bytes to int64.
but I didn't find similar function in the python.
how can I do it in the python. I just find the int.from_bytes().
input: System.Byte[], \x12\x77\x2b\xca\x9b\x62\xa2\x72\x9e\xc8\xb7\xa7\x82\xd8\x4c\xba\xcb\x41\x78\x4c\x5a\x72\xdd\xf6
output: 4666902099556679087
In python, there is only int and no int32 and int64. You can easily convert bytes string to int (supposing you are using only builtin types and not e.g. numpy):
bytesstr = bytes("123") # the bytes string
numstr = bytesstr.decode() # convert bytes string to normal string
num = int(numstr) # convert normal string to number

How do I turn a string with a comma and a dot to a float? [duplicate]

This question already has answers here:
How can I convert a string with dot and comma into a float in Python
(9 answers)
Closed 2 years ago.
I have a problem with turning a string into a float value. I'm screaming a website and trying to get the prices in to float values, but the problem is that the prices can look like this:
$2,549.98
$2,262.64
$999.00
marketprice = driver.find_element_by_xpath('/html/body/app/content-holder/marketplace-detail/landfield-detail/div/div/div[2]/div/div[1]/div[2]/span')
userprice = driver.find_element_by_xpath('/html/body/app/content-holder/marketplace-detail/landfield-detail/div/div/div[2]/div/div[1]/div[6]/span')
print(marketprice.text, userprice.text)
imarketprice = float(marketprice.text[1:])
iuserprice = float(userprice.text[1:])
When I try to convert the error I get:
ValueError: could not convert string to float: '2,549.98'
Is the problem with it that there are a comma and a dot?
Just remove the commas using:
imarketprice = float(marketprice.text[1:].replace(",", ""))

How to convert bytearray to string in python [duplicate]

This question already has answers here:
How to convert bytearray with non-ASCII bytes to string in python?
(4 answers)
Closed 4 years ago.
I need to convert the next bytearray to string:
Num = bytearray()
I have tried
Num = bytearray(str)
but that's not the solution
As t.m.adam said in the comments, use bytearray.decode
b = bytearray("test", encoding="utf-8")
b.decode()
#> 'test'

How to convert str to hex via python [duplicate]

This question already has answers here:
Print a string as hexadecimal bytes
(13 answers)
Closed 7 years ago.
I want to convert string to a hex (decimal),
for example abc to 616263 and so on.
How do I do that?
I have tried:
x= input("Enter string")
x = int(x)
hex(x)
But it is not working.
maybe you are looking for something like:
x.encode("hex")

Categories