Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python get N characters from string with specific substring

I have a very long string that I extracted from a image file.
The string can look like this

...\n\nDate: 01.01.2022\n\nArticle-no: 123456789\n\nArticle description: asdfqwer 1234...\n...

How do I extract just the 10 characters after the substring "Article-no:"?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried solving it with a different approach using rfind like this but it tends to fail every now and then if the start and end string is not accurate.

    s = "... string shown above ..."
    start = "Article-no: "
    end = "Article description: "
    print(s[s.find(start)+len(start):s.rfind(end)])

>Solution :

you can use split:

string.split("Article-no: ", 1)[1][0:10]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading