Extract a number within a string
Question:
I have a string that looks like this:
T/12345/C
T/153460/613
I would like to extract the number between the slash /
i.e. 12345
and 153460
. Sometimes I have 5 numbers, sometimes 6 or more.
I tried df[2:7]
which extracts from the second to the seventh element but i don’t know where to stop as sometimes I have 5 or 6 numbers.
How can I extract the number please?
Answers:
# Convert the string to a list.
string = "T/12345/C"
lyst = string.split("/")
print(lyst)
# Get number from the list
number = lyst[1]
print(number)
string = 'T/153460/613'
lyst = string.split("/")
print(lyst)
# Get number from the list
number = lyst[1]
print(number)
Output
['T', '12345', 'C']
12345
['T', '153460', '613']
153460
I have a string that looks like this:
T/12345/C
T/153460/613
I would like to extract the number between the slash /
i.e. 12345
and 153460
. Sometimes I have 5 numbers, sometimes 6 or more.
I tried df[2:7]
which extracts from the second to the seventh element but i don’t know where to stop as sometimes I have 5 or 6 numbers.
How can I extract the number please?
# Convert the string to a list.
string = "T/12345/C"
lyst = string.split("/")
print(lyst)
# Get number from the list
number = lyst[1]
print(number)
string = 'T/153460/613'
lyst = string.split("/")
print(lyst)
# Get number from the list
number = lyst[1]
print(number)
Output
['T', '12345', 'C']
12345
['T', '153460', '613']
153460