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?

Asked By: bravopapa

||

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
Answered By: Carl_M
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.