user308827
user308827

Reputation: 21971

Python formatted output as float of empty string

aa = ''

out_str = '%6.0f' % aa

print out_str

Is there a way to print an empty string with the formatting shown above? The reason why I need this is because the variable aa can sometimes store an actual float and other times it will be empty. But i do need to output it in the specified format. float(aa) does not work on an empty string.

EDIT:

How about the following?

aa = ''
ab = 23

out_str = '%6.0f%6.0f' % (aa,ab)

print out_str

EDIT: In above example, aa = '', ab=23 would have to print six spaces, followed by 23 with four leading spaces

Upvotes: 0

Views: 617

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49318

aa = ''
ab = 23
out_str = ''.join(('%6.0f' % var if var!='' else ' '*6) for var in (aa, ab))
print out_str

Upvotes: 3

Related Questions