Moataz Elmasry
Moataz Elmasry

Reputation: 548

Python's equivalent to C# Hash (#) in a format string

In C# and other languages, a hash (#) in the format string will be replaced by a number if one exists, and nothing if it doesn't. So a string such as:

number1 = 12.3456
number2 = 12.3
String.Format("0.00####", number1)
String.Format("0.00####", number2)

Will output number1 = 12.3456 but number2 = 12.30. A zero in the format string means if there isn't enough decimal numbers, a zero will be printed out instead.

My question is, is there a similar functionality in Python? I know I can use "{:.6F}" to format a number to 6 decimal points.

Upvotes: 3

Views: 943

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

try this:

print("{:0<.6f} {:0<.1f}".format(12.355, 0.12345))

Output:

12.355000 0.1

Upvotes: 3

Related Questions