python 占位符_Python输出格式

027dc9d14fb3485870f5402dc07ddeaf.png

有几种方式可以显示程序的输出,可以以人类可读的形式打印数据,也可以将数据写入文件以备将来使用。有时,用户经常希望控制输出的格式,而不是简单地打印以空格分隔的值。有几种格式化输出的方法。

1)若要使用格式化的字符串文字,请在开头的引号或三引号之前使用f或F开头的字符串。

2)字符串的str.format()方法可帮助用户获得更出色的输出。

3)用户可以通过使用字符串切片和串联操作来创建用户所需的任何布局,从而完成所有字符串处理。字符串类型有一些方法可以执行有用的操作,以将字符串填充到给定的列宽。

使用String模运算符(%)格式化输出

%运算符也可以用于字符串格式化。它解释左参数很像要应用于右参数的printf()样式格式字符串。在Python中,没有printf()函数,但旧的printf的功能包含在Python中。为此,字符串类将模运算符%重载以执行字符串格式化。因此,它通常称为字符串模(或有时甚至称为模数)运算符。

字符串模运算符(%)在Python(3.x)中仍然可用,并且用户正在广泛使用它。 但是如今,格式化的旧样式已从该语言中删除。

# 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.33Total students : 240, Boys : 1200313.561E+02
9580c33d45650690d921821a2eb277ae.png

在我们的示例中有两个:“%2d”和“%5.2f”。格式占位符的常规语法为:

%[flags][width][.precision]type

让我们看一下示例中的占位符。

1)第一个占位符“%2d”用于元组的第一个组成部分,即整数1。该数字将打印2个字符。由于1仅由一位数字组成,因此输出将用1个前导空格填充。

2)第二个“%8.2f”是浮点数的格式描述。与其他占位符一样,它以%字符引入。 其次是字符串应包含的总位数。该数字包括小数点和所有数字,即小数点之前和之后的数字。

3)我们的浮点数05.333必须使用5个字符格式化。数字或精度的小数部分设置为2,即“.”后面的数字在我们的占位符中。最后,占位符的最后一个字符“ f”代表“ float”。

使用格式化方法格式化输出

在Python(2.6)中添加了format()方法。字符串的格式化方法需要更多的人工。 用户使用{}来标记变量将被替换的位置,并可以提供详细的格式化指令,但是用户还需要提供要格式化的信息。通过此方法,我们可以通过位置格式将输出中的元素连接起来。

例如:

# 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 PortalPortal and Geeks

其中的方括号和字符(称为格式字段)被传递给format()方法的对象所代替。括号中的数字可用于引用传递给format()方法的对象的位置。

代码2:

# 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.55Second argument:  11, first one:   47.42Geeks:   453, Portal:    59.06

下图和示例用法描述了format方法如何处理位置参数:

742c7640984247f460a8c9f956d07035.png

代码3:

# 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 = dict(fun ="GeeksForGeeks", adj ="Portal")   # using format() in dictionary print("I love {fun} computer {adj}".format(**data)) 

输出:

Geeks: 4127; For: 4098; Geeks: 8637678I love GeeksForGeeks computer Portal

使用String方法格式化输出

在此输出中,使用字符串切片和连接操作来格式化。字符串类型有一些方法可以帮助以一种更奇妙的方式格式化输出。一些有助于格式化输出的方法是str.ljust(),str.rjust(),str.centre()

# Python program to # format a output using # string() method   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 (lstr.ljust(40, '-'))   # Printing the right aligned string  # with "-" padding   print ("The right aligned string is : ")  print (rstr.rjust(40, '-')) 

输出:

Center aligned string with fillchr: ##########I love geeksforgeeks##########The left aligned string is : I love geeksforgeeks--------------------The right aligned string is : --------------------I love geeksforgeeks
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值