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

How can I replace the "." in a float with "_" in python?

Python code (can’t change this):

import numpy as np
        
for VALUES in np.arange(0.1, 0.3, 0.1):
    print("Value: %s" % (VALUES))

Output:

Value: 0.1
Value: 0.2

Desired output:

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

Value: 0_1
Value: 0_2

I am a beginner when it comes to python, and I’m struggling with this simple task. Is this possible with the replace() method? Do I need to create a new function that makes the replacement?

>Solution :

It’s hard to tell given the limited information provided.

But if you want to make the replacement just for the code shown here you can do this:

import numpy as np
        
for VALUES in np.arange(0.1, 0.3, 0.1):
    print("Value: %s" % (str(VALUES).replace(".", "_")))

But I am not sure what you mean when you say "can’t change this".

Hope this gives a hint at least.

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