程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How does python align the numbers output by newlines

編輯:Python

Hello everyone, meet again, I'm your friend Quanstack Jun.

print

In fact, it was a very simple function, but I forgot how to wrap the output every time I use it, so I think it's better to make a record myself, so as not to check it every time.

Print function usage:

print(value, …, sep=’ ’, end=’\n’, file=sys.stdout, flush=False)

Prints the values ​​to a stream, or to sys.stdout by default.

Optional keyword arguments:

file: a file-like object (stream); defaults to the current sys.stdout.

sep: string inserted between values, default a space.

end: string appended after the last value, default a newline.

flush: whether to forcibly flush the stream.

It can be seen from the above that as long as the sep parameter is set to a newline, the output can be newline. The following is a small chestnut:

l = [(1, 2), (3, 4)]

d0 = dict((key, value) for (key, value) in l)

d1 = {key: value for (key, value) in l}

print(d0, d1, sep='\n')

#Output:

{1: 2, 3: 4}

{1: 2, 3: 4}

format

”’

Alignment of strings of desired length can be specified:

< (default) left-aligned

> right-aligned

^ center alignment

= (for numbers only) padded after the decimal point

”’

print("{0:<20}{1:<20}{2:<8}{3:<8}".format(gene_id, p.group(), p.span()[0], p.span()[1]))

"' formatting specifiers can contain a presentation type to control formatting.

For example, floating point numbers can be formatted in general format or in powers.

'b' - binary.Output the number in base 2.

‘c’ – character.Convert the integer to the corresponding Unicode string before printing.

‘d’ – Decimal integer.Print the number in base 10.

‘o’ – Octal.Output the number in base 8.

'x' - Hexadecimal.The number is output in base 16, with lowercase letters for digits above 9.

‘e’ – Power symbol.Print numbers in scientific notation.Use 'e' for power.

‘g’ – General format.Output the value in fixed-point format.When the value is particularly large, it is printed in power form.

'n' - Number.Same as 'd' when the value is an integer, same as 'g' when the value is a float.The difference is that it inserts a number separator based on the locale.

‘%’ – Percentage.Multiply the value by 100 and print it in fixed-point('f') format with a percent sign after the value.

”’

print '6:\t|{0:b}'.format(3)

print ‘7:\t|{0:c}’.format(3)

print '8:\t|{0:d}'.format(3)

print '9:\t|{0:o}'.format(3)

print '10:\t|{0:x}'.format(3)

print '11:\t|{0:e}'.format(3.75)

print '12:\t|{0:g}'.format(3.75)

print '13:\t|{0:n}'.format(3.75) #float

print '14:\t|{0:n}'.format(3) #integer

print '15:\t|{0:%}'.format(3.75)

If you want {} to represent itself rather than as a placeholder, you can use braces to escape, i.e. { {}}

Publisher: Full stack programmer, please indicate the source: https://javaforall.cn/128453.htmlOriginal link: https://javaforall.cn


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved