前言:
有几种方法可以显示程序的输出。 数据可以以人类可读的形式打印,或写入文件以供将来使用,甚至可以以某种其他指定的形式。 用户通常希望对输出格式进行更多控制,而不是简单地打印以空格分隔的值。 有几种方法可以格式化输出。
要使用格式化字符串文字,请在左引号或三引号之前以 f 或 F 开始字符串。 字符串的 format() 方法可帮助用户创建更精美的输出。 用户可以通过使用字符串切片和连接操作来完成所有字符串处理,以创建用户想要的任何布局。 string 类型有一些方法可以执行有用的操作,将字符串填充到给定的列宽。
1 使用字符串模运算符(%)格式化输出
% 运算符也可用于字符串格式化。 它将左参数解释为与 C 语言字符串中的 printf() 样式格式非常相似,以应用于右参数。在 Python 中,没有 printf() 函数,但古老的 printf 的功能包含在 Python 中。 为此,字符串类重载了模运算符 % 以执行字符串格式化。 因此,它通常被称为字符串取模(有时甚至称为模数)运算符。
字符串模运算符 ( % ) 在 Python(3.x) 中仍然可用并且被广泛使用。 但如今,旧式格式已从语言中删除。
# print integer and float value print("Geeks : %2d, Portal : %5.2f" % (1, 05.333)) ? # print integer value print("Total students : %3d, Boys : %2d" % (240, 120)) ? # print octal value print("%7.3o" % (25)) ? # print exponential value print("%10.3E" % (356.08977))
输出:
在我们的示例中有两个: [%2d]和[%5.2f]。 格式占位符的一般语法是: %[flags][width][.precision]type
让我们看一下示例中的占位符。
第一个占位符[%2d]用于我们元组的第一个组件,即整数 1。该数字将打印 2 个字符。 由于 1 仅包含一位数字,因此输出用 1 个前导空白填充。 第二个[%5.2f]是浮点数的格式描述。 与其他占位符一样,它以 % 字符引入。 后面是字符串应包含的总位数。 这个数字包括小数点和所有数字,即小数点之前和之后。 我们的浮点数 05.333 必须格式化为 5 个字符。 数字的小数部分或精度设置为 2,即[.]后面的数字。 在我们的占位符中。 最后,占位符的最后一个字符[f]代表[float]。
2 使用 format 方法格式化输出
在 Python(2.6) 中添加了 format() 方法。 字符串的格式化方法需要更多的人工。用户使用 {} 标记变量将被替换的位置,并且可以提供详细的格式化指令,但用户还需要提供要格式化的信息。 此方法允许我们通过位置格式连接输出中的元素。如下例所示:
例一:
# using format() method print('I love {} for "{}!"'.format('Geeks', 'Geeks')) ? # using format() method and referring # a position of the object print('{0} and {1}'.format('Geeks', 'Portal')) ? print('{1} and {0}'.format('Geeks', 'Portal')) ? ? # the above formatting can also be done by using f-Strings # Although, this features work only with python 3.6 or above. ? print(f"I love {'Geeks'} for \"{'Geeks'}!\"") ? # using format() method and referring # a position of the object print(f"{'Geeks'} and {'Portal'}")
输出:
其中的括号和字符(称为格式字段)被传递给 format() 方法的对象替换。 括号中的数字可用于表示传递给 format() 方法的对象的位置。
?例二:
# combining positional and keyword arguments print('Number one portal is {0}, {1}, and {other}.' ? ? ?.format('Geeks', 'For', other ='Geeks')) ? # using format() method with number print("Geeks :{0:2d}, Portal :{1:8.2f}". ? ? ? format(12, 00.546)) ? # Changing positional argument print("Second argument: {1:3d}, first one: {0:7.2f}". ? ? ? format(47.42, 11)) ? print("Geeks: {a:5d}, ?Portal: {p:8.2f}". ? ? ?format(a = 453, p = 59.058))
输出:
例三:
tab = {'geeks': 4127, 'for': 4098, 'geek': 8637678} ? # using format() in dictionary print('Geeks: {0[geeks]:d}; For: {0[for]:d}; ' ? ? 'Geeks: {0[geek]:d}'.format(tab)) ? data = dict(fun ="GeeksForGeeks", adj ="Portal") ? # using format() in dictionary print("I love {fun} computer {adj}".format(**data))
输出:
?3 使用 String 方法格式化输出
此输出通过使用字符串切片和连接操作进行格式化。 字符串类型有一些方法可以帮助以更奇特的方式格式化输出。 一些有助于格式化输出的方法是 str.rjust()、str.rjust() 和 str.centre()。
cstr = "I love geeksforgeeks" ? ? # Printing the center aligned? # string with fillchr print ("Center aligned string with fillchr: ") print (cstr.center(40, '#')) ? # Printing the left aligned? # string with "-" padding? print ("The left aligned string is : ") print (cstr.ljust(40, '-')) ? # Printing the right aligned string # with "-" padding? print ("The right aligned string is : ") print (cstr.rjust(40, '-'))
输出:
到此这篇关于python中的格式化输出方法的文章就介绍到这了,更多相关python格式化输出内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!