Python right alignment example method

For example, there is a dictionary as follows:

 dic ={"name":"botoo","url":"//www.zalou.cn","page":"88","isNonProfit":"true","address":"china",}

The desired output is as follows:

name:botoo
url:https:www.zalou.cn
page:88
isNonProfit:ture
address:china

First get the maximum value of the dictionary max(map(len, dic.keys()))

Then use

Str.rjust() align right

or

Str.ljust() left justify

or

Str.center() The center method has sequential output.

 dic ={"name":"botoo","url":"//www.zalou.cn","page":"88","isNonProfit":"true","address":"china",}
    
 d =max(map(len, dic.keys())) #Get the maximum value of key
    
 for k in dic:print(k.ljust(d),":",dic[k])
   
name    : botoo
url     ://www.zalou.cn
page    :88
isNonProfit :true
address   : china
 for k in dic:print(k.rjust(d),":",dic[k])
   
 name : botoo
 url ://www.zalou.cn
 page :88
isNonProfit :true
 address : china
 for k in dic:print(k.center(d),":",dic[k])
   
 name  : botoo
 url   ://www.zalou.cn
 page  :88
isNonProfit :true
 address  : china

The usage of str.ljust() is like this;

 s ="adc"
 s.ljust(20,"+")'adc+++++++++++++++++'
 s.rjust(20)'adc'
 s.center(20,"+")'++++++++adc+++++++++'

Knowledge point expansion:

Alignment of strings in python

ljust(), rjust() and center() functions represent left, right, and center alignment, respectively

str.ljust(width[, fillchar]): left justify, width — specify the length of the string, fillchar — fill character, the default is a space;
str.rjust(width[, fillchar]): right justify, width — specify the length of the string, fillchar — fill character, the default is a space;
str.center(width[, fillchar]): center alignment, width — the total width of the string, fillchar — the filling character, the default is a space.

test ='hello world'print(test.ljust(20))print(test.ljust(20,'*'))print(test.rjust(20,'*'))print(test.center(20,'*'))print(test.center(20))
 
# The output results are as follows:
hello world******************hello world
****hello world*****
 hello world   

So far, this article on the example method of python right alignment is introduced. For more related content on how to right alignment in python, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. I hope you will support ZaLou more in the future. Cn!

Recommended Posts

Python right alignment example method
Python3.7 debugging example method
Python object-oriented example
Example method of reading sql from python
Python example method to open music files
Python magic method topic
Python object-oriented magic method
Python function-dictionary get() method
Python error handling method
Python interpolate interpolation example
Python defines a function method
Python TCP packet injection method
Python drawing ring graph method
Python tracking except information method
Python3 logging log package example
Python regular expression example code
Python output mathematical symbols example
Python implements gradient descent method
Python one-dimensional two-dimensional interpolation example
Python draw bar graph (bar graph) example
Python method of parameter passing
Python arithmetic sequence calculation method
Method steps to increase python font
Python regular expression learning small example
Python and js interactive call method
Python calculation of information entropy example
Python waterfall line indicator writing example
Python list comprehension operation example summary
Python ATM function implementation code example
Python example to calculate IV value
Python implements the steepest descent method
Python decorator simple usage example summary
Example operation of python access Alipay
Python tcp transmission code example analysis
End the method of running python
Python requests module session code example
Python verification code interception identification code example