0

I have a string "Halo Device ANumber : 6232123123 Customer ID : 16926"
I want to search with this keyword : "62"
Then if there any, I want to show it's whole substring "6232123123"

string = str('Halo Device ANumber : 6232123123 Customer ID : 16926')
ind = string.find('62')
word = string.split()[ind]

print('62 found in index: ', ind)
print('substring : ', word)

output :

62 found in index:  22
substring : Device
user3679987
  • 27
  • 1
  • 7

1 Answers1

1

If you're looking for a particular "full string", where it ends when there is simply a space, you can just use .find() again:

string = str('Halo Device ANumber : 6232123123 Customer ID : 16926')
target = '62'
ind = string.find(target)

ind_end = string.find(' ', ind + len(target))
word = string[ind:ind_end]

Now, if you wanted something more robust, we can use regex:

string = str('Halo Device ANumber : 6232123123 Customer ID : 16926')
word = re.findall('(62\d+)', string).groups()[0]

This will take the first match from your string which starts with "62" and captures all remaining numerical digits.

M Z
  • 4,571
  • 2
  • 13
  • 27