Is there a way to extract only numbers before and after certain characters in Python?

Asked 2 years ago, Updated 2 years ago, 134 views

number = '9-11'

number1 = '10-30'

In Python, how do I print only the values in front of it, and the values in the back? However, since the units of numbers are different like 9 and 10, it is not extracted as a position value, but

Is there a way to extract the number before the '-' from 9-11, or the number after the '-

I tried typing "*-" like Excel, but it didn't work.T

I ask for your help me.

slice

2022-09-20 19:17

2 Answers

>>> "9-11".split("-")
['9', '11']
>>> [ int(s) for s in "9-11".split("-") ]
[9, 11]
>>> map(int, "9-11".split("-"))
<map object at 0x00000289A41F44C0>
>>> tuple(map(int, "9-11".split("-")))
(9, 11)
>>> tuple(map(int, "10-30".split("-")))
(10, 30)
>>> tuple(map(int, "1-2-3-4-30".split("-")))
(1, 2, 3, 4, 30)
>>> 


2022-09-20 19:17

Thank you so much for your answer. There's no place to ask I was struggling by myself and cried. I'll deal with it bravely from now on. ^ Thanks once again.

^


2022-09-20 19:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.