How do I print a '%' sign using string formatting?

后端 未结 5 954
孤城傲影
孤城傲影 2020-12-30 20:32

I\'ve made a little script to calculator percent; however, I wish to actually include the \'%\' within the message printed...

Tried this at the start - didn\'t work.

5条回答
  •  离开以前
    2020-12-30 21:07

    x = 0.25
    y = -0.25
    print("\nOriginal Number: ", x)
    print("Formatted Number with percentage: "+"{:.2%}".format(x));
    print("Original Number: ", y)
    print("Formatted Number with percentage: "+"{:.2%}".format(y));
    print()
    

    Sample Output:

    Original Number:  0.25                                                                                        
    Formatted Number with percentage: 25.00%                                                                      
    Original Number:  -0.25                                                                                       
    Formatted Number with percentage: -25.00% 
    

    Helps in proper formatting of percentage value

    +++

    Using ascii value of percentage - which is 37

    print( '12' + str(chr(37)) )
    

提交回复
热议问题