Issue
I want to print some sequence of number in a specific format. Like if there are 3 digits it will print the integer but if there are not 3 digits for example if there are two digits or one digit then it will make up spaces. For example:
__3 bench
_55 pencils
675 pens
I wanted to how can I format this. the numbers are always integers.
for key, value in dict.items():
print(f'{key:} {value}')
Solution
Something like this by giving width
to your parameter to print:
for key, value in dict.items():
print (f'{key:>3} {value}')
Output:
>
3 bench
45 pencils
675 pens
You can replace 3 with the maximum number of digits you might have in your data or even pass it dynamically :
width = 3
for key, value in dict.items():
print (f'{key:>{width}} {value}')
Answered By - eshirvana Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.