Python如何使用输出格式(代码示例)

本文概述

  • Python3
  • Python3
  • Python3
  • Python3
  • Python3
有几种方式可以显示程序的输出, 可以以人类可读的形式打印数据, 也可以将数据写入文件以备将来使用。有时, 用户通常希望更多地控制输出的格式, 而不是简单地打印以空格分隔的值。有几种格式化输出的方法。
  • 使用格式化的字符串文字, 请在开头的引号或三引号之前使用f或F开头的字符串。
  • 的str.format()字符串方法可帮助用户获得更出色的输出
  • 用户可以使用字符串切片和连接操作来创建用户所需的任何布局, 从而完成所有字符串处理。字符串类型有一些方法可以执行有用的操作, 以将字符串填充到给定的列宽。
使用String模运算符(%)格式化输出:
%运算符还可以用于字符串格式化。它解释左参数很像要应用于右参数的printf()样式格式字符串。在Python中, 没有printf()函数, 但旧的printf的功能包含在Python中。为此, 字符串类将模运算符%重载以执行字符串格式化。因此, 它通常被称为字符串模(或有时甚至称为模数)运算符。
字符串模运算符(%)在Python(3.x)中仍然可用, 并且用户正在广泛使用它。但是如今, 格式化的旧样式已从该语言中删除。
Python3
# Python program showing how to use # string modulo operator(%) to print # fancier output # 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 ))

输出:
Geeks :1, Portal : 5.33 Total students : 240, Boys : 120 031 3.561E+02

Python如何使用输出格式(代码示例)

文章图片
在我们的示例中有两个:"%2d"和"%5.2f"。格式占位符的常规语法为:
%[flags][width][.precision]type

让我们看一下示例中的占位符。
  • 第一个占位符"%2d"用于元组的第一个组成部分, 即整数1。该数字将打印2个字符。由于1仅包含一位数字, 因此输出将填充1个前导空格。
  • 第二个"%5.2f"是浮点数的格式描述。与其他占位符一样, 它以%字符引入。其次是字符串应包含的总位数。该数字包括小数点和所有数字, 即小数点之前和之后的数字。
  • 我们的浮点数05.333必须使用5个字符格式化。数字或精度的小数部分设置为2, 即"。"后面的数字在我们的占位符中。最后, 占位符的最后一个字符" f"代表" float"。
使用format方法格式化输出:
在Python(2.6)中添加了format()方法。字符串的格式化方法需要更多的人工。用户使用{}标记变量将被替换的位置, 并可以提供详细的格式指令, 但是用户还需要提供要格式化的信息。通过此方法, 我们可以通过位置格式将输出中的元素连接起来。例如 -
代码1:
Python3
# Python program showing # use of format() method # using format() method print ( 'I love {} for "{}!"' . format ( 'Geeks' , 'Geeks' )) # using format() method and refering # a position of the object print ( '{0} and {1}' . format ( 'Geeks' , 'Portal' )) print ( '{1} and {0}' . format ( 'Geeks' , 'Portal' ))

输出:
I love Geeks for "Geeks!" Geeks and Portal Portal and Geeks

其中的方括号和字符(称为
格式栏位
【Python如何使用输出格式(代码示例)】)替换为传递给format()方法的对象。括号中的数字可用于引用传递给format()方法的对象的位置。
代码2:
Python3
# Python program showing # a use of format() method # 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 ))

输出如下:
Number one portal is Geeks, For, and Geeks. Geeks :12, Portal :0.55 Second argument:11, first one:47.42 Geeks:453, Portal:59.06

下图和一个示例用法描述了format方法如何处理位置参数:
Python如何使用输出格式(代码示例)

文章图片
代码3:
Python3
# Python program to # show format () is # used in dictionary 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 = https://www.lsbin.com/dict (fun ="lsbin" , adj = "Portal" ) # using format() in dictionary print ( "I love {fun} computer {adj}" . format ( * * data))

输出如下:
Geeks: 4127; For: 4098; Geeks: 8637678 I love lsbin computer Portal

使用String方法格式化输出:
通过使用字符串切片和串联操作来格式化此输出。字符串类型具有一些方法, 可以帮助以一种更奇妙的方式格式化输出。一些有助于格式化输出的方法是
str.ljust()
,
str.rjust()
,
str.centre()
Python3
# Python program to # format a output using # string() method cstr = "I love lsbin"# 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 , '-' ))

输出如下:
Center aligned string with fillchr: ##########I love lsbin##########The left aligned string is : I love lsbin--------------------The right aligned string is : --------------------I love lsbin

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

    推荐阅读