简体   繁体   English

Python:print()语句中的%运算符

[英]Python: % operator in print() statement

I just came across this Python code, my question is about the syntax in the print statement: 我刚刚遇到这个Python代码,我的问题是关于print语句中的语法:

class Point(object):
    """blub"""
    #class variables and methods

blank = Point
blank.x = 3.0
blank.y = 4.0    
print('(%g,%g)' % (blank.x,blank.y))

This print statement simply prints (3.0, 4.0) , ie the values in blank.x and blank.y. 此print语句只打印 3.0,4.0 ,即blank.x和blank.y中的值。

I don't understand the % operator in front of the (blank.x, blank.y) in the last line. 我不理解最后一行(blank.x,blank.y)前面的运算符。 What does it do and where can I find it in the documentation? 它做了什么以及在哪里可以在文档中找到它?

Googling this, I always end up with the modulus operator. 谷歌搜索这个,我总是以模数运算符结束。

'(%g,%g)' is the template and (blank.x,blank.y) are the values which need to be filled in place of %g and %g and % operator does just that. '(%g,%g)'是模板, (blank.x,blank.y)是需要填充的值,而不是%g%g%运算符就是这样。 Its called String interpolation or formatting operator . 它叫做String插值或格式化运算符

Intro 介绍

The % operator in python for strings is used for something called string substitution. python中字符串的%运算符用于称为字符串替换的东西。 String and Unicode objects have one unique built-in operation: the % operator (modulo). String和Unicode对象有一个独特的内置操作:%operator(modulo)。

This is also known as the string formatting or interpolation operator. 这也称为字符串格式或插值运算符。

Given format % values (where format is a string or Unicode object), % conversion specifications in format are replaced with zero or more elements of values. 给定格式%值(其中format是字符串或Unicode对象),格式的%转换规范将替换为零个或多个值元素。 The effect is similar to the using sprintf() in the C language. 效果类似于在C语言中使用sprintf()。

If format is a Unicode object, or if any of the objects being converted using the %s conversion are Unicode objects, the result will also be a Unicode object. 如果format是Unicode对象,或者使用%s转换转换的任何对象是Unicode对象,则结果也将是Unicode对象。


Usage 用法

'd' Signed integer decimal.  
'i' Signed integer decimal.  
'o' Signed octal value. (1)
'u' Obsolete type – it is identical to 'd'. (7)
'x' Signed hexadecimal (lowercase). (2)
'X' Signed hexadecimal (uppercase). (2)
'e' Floating point exponential format (lowercase).  (3)
'E' Floating point exponential format (uppercase).  (3)
'f' Floating point decimal format.  (3)
'F' Floating point decimal format.  (3)
'g' Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.  (4)
'G' Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.  (4)
'c' Single character (accepts integer or single character string).   
'r' String (converts any Python object using repr()).   (5)
's' String (converts any Python object using str()).    (6)
'%' No argument is converted, results in a '%' character in the result.

These are the values that can be substituted. 这些是可以替代的值。 To substitute, you simply follow the following syntax: 要替换,只需遵循以下语法:

string%values #where string is a str or unicode and values is a dict or a single value

Examples 例子

>>> print '%(language)s has %(number)03d quote types.' % \
...       {"language": "Python", "number": 2}
Python has 002 quote types.

>>> print "My name is %s"%'Anshuman'
My name is Anshuman

>>> print 'I am %d years old'%14
I am 14 years old

>>> print 'I am %f years old' % 14.1
I am 14.1 years old

another example: 另一个例子:

def greet(name):
    print 'Hello, %s!'%name

Caution! 警告!

A conversion specifier contains two or more characters and has the following components, which must occur in this order: 转换说明符包含两个或多个字符,并具有以下组件,这些组件必须按以下顺序出现:

  • The '%' character, which marks the start of the specifier. '%'字符,标记说明符的开头。
  • Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)). 映射键(可选),由带括号的字符序列组成(例如,(somename))。
  • Conversion flags (optional), which affect the result of some conversion types. 转换标志(可选),它会影响某些转换类型的结果。
  • Minimum field width (optional). 最小字段宽度(可选)。 If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision. 如果指定为'*'(星号),则从值的元组的下一个元素读取实际宽度,并且要转换的对象在最小字段宽度和可选精度之后。
  • Precision (optional), given as a '.' 精度(可选),以'。'给出 (dot) followed by the precision. (点)后跟精度。 If specified as '*' (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision. 如果指定为'*'(星号),则从值的元组的下一个元素读取实际宽度,并且要转换的值在精度之后。
  • Length modifier (optional). 长度修饰符(可选)。
  • Conversion type. 转换类型。

References 参考

它只是告诉Python替换格式化部分中的所有%: http//docs.python.org/2/library/stdtypes.html#string-formatting-operations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM