在Python中使用逗号,串联和字符串格式化程序之间的区别

Poo*_*oja 27 python string

我正在学习python(2.7).我了解到我们可以使用以下方法将字符串和变量放在一起打印:

x = "Hello"
y = "World"
Run Code Online (Sandbox Code Playgroud)

使用逗号:

print "I am printing" , x, y  # I know that using comma gives automatic space
Run Code Online (Sandbox Code Playgroud)

通过使用串联:

print "I am printing" + " " + x + " " + y 
Run Code Online (Sandbox Code Playgroud)

通过使用字符串格式化器

print "I am printing %s %s" % (x, y) 
Run Code Online (Sandbox Code Playgroud)

在这种情况下,所有三个打印相同:

I am printing Hello World
Run Code Online (Sandbox Code Playgroud)

这三者之间有什么区别,是否存在一个优先于另一个的特定情况?

Aar*_*all 43

要首先回答一般问题,您通常会在编写代码时使用打印功能将脚本中的信息输出到屏幕,以确保获得预期效果.

随着您的代码变得越来越复杂,您可能会发现日志记录比打印更好,但这是另一个答案的信息.

打印和返回值表示之间存在很大差异,这些表示在与Python解释器的交互式会话中回显.打印应打印到您的标准输出.表达式返回值的回显表示(如果没有,则显示在Python shell中None)将在脚本中运行等效代码时保持静默.

1.印刷

在Python 2中,我们有print语句.在Python 3中,我们得到了一个print函数,我们也可以在Python 2中使用它.

使用逗号打印语句(Python 2)

带有逗号分隔项的print语句,使用空格分隔它们.尾随逗号将导致附加另一个空格.没有尾随逗号会附加要添加到打印项目的换行符.

您可以将每个项目放在单独的print语句中,并在每个项目之后使用逗号,它们将在同一行上打印相同的内容.

例如(这只适用于脚本,在交互式shell中,每行后都会得到一个新提示):

x = "Hello"
y = "World"

print "I am printing",
print x,
print y 
Run Code Online (Sandbox Code Playgroud)

输出:

I am printing Hello World
Run Code Online (Sandbox Code Playgroud)

打印功能

使用Python 3的内置打印功能,也可以在Python 2.6和2.7中使用此导入:

from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)

你可以声明一个分隔符和一个结尾,这给了我们更大的灵活性:

>>> print('hello', 'world', sep='-', end='\n****\n')
hello-world
****
>>>
Run Code Online (Sandbox Code Playgroud)

默认' '值为sep和'\n'end:

>>> print('hello', 'world')
hello world
>>> 
Run Code Online (Sandbox Code Playgroud)

2.字符串连接

连接在内存中创建每个字符串,然后在它们的末尾将它们组合在一个新字符串中(因此这可能不是非常友好的内存),然后同时将它们打印到输出中.当您需要将可能在其他地方构建的字符串连接在一起时,这很好.

print('hello' + '-' + 'world')
Run Code Online (Sandbox Code Playgroud)

将打印

hello-world
Run Code Online (Sandbox Code Playgroud)

在尝试以这种方式将其他类型的文字连接到字符串之前要小心,首先将文字转换为字符串.

print('here is a number: ' + str(2))
Run Code Online (Sandbox Code Playgroud)

版画

here is a number: 2
Run Code Online (Sandbox Code Playgroud)

如果您尝试连接整数而不首先将其强制转换为字符串:

>>> print('here is a number: ' + 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)

这应该表明你应该只尝试连接已知为字符串的变量.新的格式化方式接下来为您处理此问题.

3.字符串插值

您演示的格式是从C借用的旧式字​​符串插值.它采用旧字符串,一次创建一个新字符串.它的作用相当简单.当你看起来可能会建立一个相当大的模板时,你应该使用它(在3+行和3+变量上,你绝对应该这样做).

这样做的新方法是(使用参数的索引):

print('I am printing {0} and {1}'.format(x, y))
Run Code Online (Sandbox Code Playgroud)

或者在python 2.7或3中(使用隐含索引):

print('I am printing {} and {}'.format(x, y))
Run Code Online (Sandbox Code Playgroud)

或者使用命名参数(这在语义上很容易阅读,但代码看起来不是很干(即不要重复自己))

print('I am printing {x} and {y}'.format(x=x, y=y))
Run Code Online (Sandbox Code Playgroud)

这种%格式化格式(这里没有演示)的最大好处是它可以让你组合位置和关键字参数

print('I am printing {0} and {y}'.format(x, y=y))
Run Code Online (Sandbox Code Playgroud)

Python 3.6中的新功能,格式文字

Python 3.6将具有格式文字,具有更优雅的语法(更少的冗余).简单的语法是这样的:

print(f'I am printing {x} and {y}')
Run Code Online (Sandbox Code Playgroud)

格式文字实际上可以就地执行代码:

>>> print(f'I am printing {"hello".capitalize()} and {"Wo" + "rld"}')
I am printing Hello and World
Run Code Online (Sandbox Code Playgroud)