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

Only join number until hit any latter in python using join

string = "buy @ 1890 . 00 and exit $700"
If the string contains @ then split the string using @. Then take the last split item and only join the number until join hits the letter not the number.

price = ''.join(c for c in string.replace(" ","").split("@")[-1] if (c.isdigit() or c=="." ))

but it retunes 1890.00700, not 1890.00

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

desire output only 1890.00, not 1890.00700

>Solution :

If you are open to using regex, re.findall might work well here:

string = "buy @ 1890 . 00 and exit $700"
nums = re.findall(r'@\s*([0-9. ]+)\b', string)
print(nums)  # ['1890 . 00']
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