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

Destroy 2 caracters after a number

I need help, in a long string, I want to know the the 2 characters after a number

example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
if in example after 1 number == "gr":
so delete "gr"

>Solution :

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

If I understand your question correctly, what you need is regex:

import re
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
re.sub("(\d)gr", r"\1", example)

Output

gz15123da1az41sd23f168zgre4r6z

Explnatation

The re.sub function takes three arguments: the first argument is a patter, the second one the replacement, and the last of is the string itself. (\d)gr selects the parts that contain a number followed by gr, then replaces it with the number (\d) itself(removing the gr part).

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