Rahul Khanna
Rahul Khanna

Reputation: 335

Changing str to int in Python2

time = raw_input()
if "PM" in time:
    ls = time.split(":")
    int(ls[0])
    print type(ls[0])

I have a problem on Hackerearth which I was solving. But my code here doesn't change the str to int. The output is

<type 'str'>

I want to change the first element of the time to int so i can perform mathematical calculations on it. Input it takes is

6:05:08PM

Upvotes: 0

Views: 217

Answers (3)

Marius M
Marius M

Reputation: 496

int(ls[0]) should do the trick for you, however, note that this is a very less safe approach.

Upvotes: 0

Steve Deng Zishi
Steve Deng Zishi

Reputation: 128

Integer = int(ls[0])

It cast str to an int and then u can use this Integer to do calculations

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121446

int() returns the integer. The variable is not changed in-place.

Assign the result:

ls[0] = int(ls[0])

Upvotes: 1

Related Questions