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
4
  • What is b in your code? Commented Sep 2, 2020 at 4:02
  • Does this answer your question? Python: Find a substring in a string and returning the index of the substring Commented Sep 2, 2020 at 4:05
  • 1
    @RoshinRaphel sorry i mean ind, i 'm just edit it thanks for correction.. Commented Sep 2, 2020 at 4:08
  • @Karthik i think its not bcause i want to show whole its substring if any Commented Sep 2, 2020 at 4:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.