Reputation: 1201
I am trying to get a credit card's credential from the front-end, I am getting a 'expiry date' response in ajax 20/2020
, and i am trying to split it using python split() function.
x = 20/2020
I want result as 20 and 2020 but its coming it different ways as mentioned below, so how can i get two integers in split?
x.split() = ['20/2020']
x.split("/") = ["20","2020"]
Right now i am getting two or one string in output.
Upvotes: 2
Views: 1880
Reputation:
You said:
x.split() -> ['20/2020']
x.split("/") -> ["20","2020"]
Both of these are as expected.
str.split()
returns a list (comparable to an array in many languages).
str.split()
with no parameters will split on any whitespace, of which there is none in '20/2020'
.
str.split('/')
properly splits on the '/', giving you a list with 2 elements instead of one.
If you know your input will only ever contain a single '/', then you can get the 2 integers you need in a few ways. Here's a couple:
x = '20/2020'
x = x.split('/')
month = x[0]
year = x[1]
Or:
x = '20/2020'
month, year = x.split('/')
To understand why the second one works, you can see the answer here: Assign multiple values of a list
Upvotes: 0
Reputation: 131
Can you try doing this:
a,b = x.split('/')
This will work only if there are 2 elements after splitting.
Upvotes: 1
Reputation: 360
you can use map to get in int
>> list(map(int, x.split("/")))
>> [20, 2020]
You can change int to float if you want result in float
Upvotes: 0