We have updated our Privacy Notice, you can read the updated document here
Mods please check the Moderators Group for an important update on Mod tools. If you do not have access to the group, please PM Niamh. Thanks!

Python: formatting a messy string

  • #2
    Registered Users Posts: 4,192 ✭✭✭ iLikeWaffles


    I have this 3d printer script I'm editing to make it a little more useful, I am not finding it useful at the minute as you have to stare at a scrolling output for up to 30 seconds or more (depending on filename length) to get the information without the script being edited. The script does have an option to disable the horizontal scrolling but I plan to (eventually) periodically scroll and show a time remaining value every so often, as well switching to the filename at intervals which will require the scrolling.

    I want to format a number as a string to 1 decimal place or maybe a whole number but it is currently printing out as long. I know int() takes 2 arguments (number, base) but I do not think it will work in this case. I'm also not too sure if format() or .format can be used with the concatenation.
    max_layer = "68" #starts out as an int and is converted to string and int in multiple places 
    current_layer = i # i == loop
    
    # "Layer " + i + " of " + max_layer
    # display_text == "Layer 18 of 68"         
    
    
    display_text += " " + "(" + str( (current_layer / int(max_layer) )  * 100 ) + "%)"
    
    print(display_text)    #for this example
    
    output:
    Layer 18 of 68 (26.47058823529412%)
    

    I'd like it to output as "(26%)" or rounded to 1 decimal "(26.5%)" depending on UX. What would be the best way to format inside the concatenated string. Given that print() will not be available in the script


Comments

Society & Culture