Convert string time to float python

How can I convert a time string '15:04:23' (hours:minuts:seconds) into a float 150423, using python? The following steps had been done yet:

Initially I have the time/date information in the format seconds since 01.01.2000. I converted this using the following function

 t_temp = datetime(2000,1,1)+timedelta(seconds = t)

Then I extracted the time

t_temp.strftime("%H:%M:%S")

... which gives me string

asked Jan 26, 2019 at 11:28

4

Just omit the colons and cast the result to a float:

float(t_temp.strftime("%H%M%S"))

I don’t know how much sense this really makes, but there you go.

answered Jan 26, 2019 at 11:36

decezedeceze

497k81 gold badges719 silver badges867 bronze badges

do this

time = float(t_temp.replace(":",""))

this will remove the colons and typecast the string into float.Works in both python2 and python3

here are two useful links

answered Jan 26, 2019 at 11:34

Convert string time to float python

2

Introduction

In this article, we will use Python’s float() function to convert strings to floats. And we will also use Python’s str() function to convert floats to strings.

It is important to properly convert the data types before using them for calculations and concatenations in order to prevent runtime errors.

Using the float() function

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

Example

Let’s look at an example of converting a string to float in Python:

input_1 = '10.5674'

input_1 = float(input_1)

print(type(input_1))
print('Float Value =', input_1)

Output:

<class 'float'>
Float Value = 10.5674

The string value of '10.5674' has been converted to a float value of 10.5674.

Why do we need to convert a string to float?

If we are receiving float value from user input through the terminal or reading it from a file, then they are string objects. We have to explicitly convert them to float so that we can perform necessary operations on them, such as addition, multiplication, etc.

input_1 = input('Please enter first floating point value:\n')
input_1 = float(input_1)

input_2 = input('Please enter second floating point value:\n')
input_2 = float(input_2)

print(f'Sum of {input_1} and {input_2} is {input_1+input_2}')

Note: If you are not familiar with string formatting using f prefix, please read f-strings in Python.

Let’s run this code and provide float values for input_1 and input_2:

Please enter first floating point value:
10.234
Please enter second floating point value:
2.456
Sum of 10.234 and 2.456 is 12.69

The resulting sum of 10.234 and 2.456 is 12.69.

Ideally, we should use a try-except block to catch exceptions in case of invalid input from the user.

Using the str() function

We can also convert a float to a string using the str() function. This might be required in situations where we want to concatenate float values.

Example

Let’s look at an example:

input_1 = 10.23
input_2 = 20.34
input_3 = 30.45

# using f-string from Python 3.6+, change to format() for older versions
print(f'Concatenation of {input_1} and {input_2} is {str(input_1) + str(input_2)}')
print(f'CSV from {input_1}, {input_2} and {input_3}:\n{str(input_1)},{str(input_2)},{str(input_3)}')
print(f'CSV from {input_1}, {input_2} and {input_3}:\n{", ".join([str(input_1),str(input_2),str(input_3)])}')

Let’s run this code:

Concatenation of 10.23 and 20.34 is 10.2320.34
CSV from 10.23, 20.34 and 30.45:
10.23,20.34,30.45
CSV from 10.23, 20.34 and 30.45:
10.23, 20.34, 30.45

Concatenating 10.23 and 20.34 produces the string '10.2320.34'. This code also produces two versions of comma-separated values (CSV).

If we don’t convert float to string in the above program, the join() function will throw an exception. Also, we won’t be able to use the + operator to concatenate as it will add the floating point numbers.

Conclusion

You can check out the complete Python script and more Python examples from our GitHub repository.

References:

  • float() official documentation

How do I convert a string to a float in Python?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

How do I convert string to time in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

Is time a float in Python?

time() returns a floating-point number of seconds that have passed since a starting point of time. The starting point of time is called the epoch.

How do I convert time to seconds in Python?

How to convert a time string to seconds in Python.
time_string = "01:01:09".
date_time = datetime. datetime. strptime(time_string, "%H:%M:%S").
print(date_time).
a_timedelta = date_time - datetime. datetime(1900, 1, 1).
seconds = a_timedelta. total_seconds().
print(seconds).