PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 20, 2022

[FIXED] how to format integers so that it prints spaces before it if there are not three number

 July 20, 2022     formatting, integer, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing