python print_Python print()

python print

Hello learners. In this tutorial we are going to learn more about Python print function. In our last tutorial we learned about Python float function.

学习者们好。 在本教程中,我们将学习有关Python打印功能的更多信息。 在上一教程中,我们了解了Python float函数。

Python打印 (Python Print)

Almost all of our previous tutorial contains the Python print() function. But we did not discussed about python print function to the fullest. Now we will learn it. At first we should know about the basic structure of python print function. That is given below;

我们之前的教程几乎都包含Python print()函数。 但是我们没有充分讨论python打印功能。 现在我们将学习它。 首先,我们应该了解python打印功能的基本结构。 如下所示;

If you read our tutorial on Python Functions and arguments, then you should probably had the idea about the above function.

如果您阅读了有关Python函数和参数的教程,那么您可能应该对上述函数有所了解。

The values receives a list of undefined variables. So, all the comma separated values will goes under the list values. So if you add more elements separated by comma, you will get a output where all the values are put together separated by space. The following example will guide you about the simple python print function usage.

这些values接收未定义变量的列表。 因此,所有用逗号分隔的值都将在列表值下 。 因此,如果添加更多用逗号分隔的元素,则会得到一个输出,其中所有值都用空格分隔在一起。 以下示例将指导您有关简单python打印功能的用法。

# initialize 1st variable
var1 = 1

# initialize 2nd variable
var2 = 'string-2'

# initialize 3rd variable
var3 = float(23.42)

print(var1, var2, var3)

The output of the following code will be.

以下代码的输出将是。

1 string-2 23.42

So, as many item you want to print, just put them together as arguments.

因此,您要打印的项目很多,只需将它们作为参数放在一起即可。

在python打印功能中使用sep关键字 (Using sep Keyword in python print function)

If see the example of the previous section, you will notice that that variables are separated with a space. But you can customize it to your own style.

如果查看上一节的示例,您会注意到变量之间用空格分隔。 但是您可以根据自己的风格对其进行自定义。

Suppose in the previous code, you want to separate the values using underscore(_). Then you should pass underscore as the value of sep keyword. The following function will illustrate you the idea of using python print sep keyword.

假设在前面的代码中,您想使用下划线(_)分隔值。 然后,应将下划线作为sep关键字的值。 以下功能将说明您使用python print sep关键字的想法。

# initiaze 1st variable
var1 = 1

# initialize 2nd variable
var2 = 'string-2'

# initialize 3rd variable
var3 = float(23.42)

print(var1, var2, var3, sep='_')

And you will get your desired output like this.

这样您将获得所需的输出。

1_string-2_23.42

Python打印结束关键字 (Python print end keyword)

The end key of print function will set the string that needs to be appended when printing is done.

打印功能的结束键将设置打印完成后需要附加的字符串。

By default the end key is set by newline character. So after finishing printing all the variables, a newline character is appended. Hence, we get the output of each print statement in different line. But we will now overwrite the newline character by a hyphen(-) at the end of the print statement. See the following example.

默认情况下, 结束键由换行符设置。 因此,在完成所有变量的打印后,将添加一个换行符。 因此,我们在不同的行中获得每个打印语句的输出。 但是我们现在将在打印语句的末尾用连字符(-)覆盖换行符。 请参见以下示例。

# initialize a list
initList = ['camel', 'case', 'stop']

# print each words using loop
print('Printing using default print function')
for item in initList:
    print(item)  # default print function. newline is appended after each item.

print()  # another newline

# print each words using modified print function
print('Printing using modified print function')
for item in initList:
    print(item, end='-')

And you will get outputs like the following

您将获得类似以下的输出

Printing using default print function
camel
case
stop

Printing using modified print function
camel-case-stop-

Python打印到文件 (Python print to file)

In this section we will learn about file keyword. Actually file keyword is used for extracting output to a specified file. If you read our previous tutorial Python file operation, then you should know about basic file operation.

在本节中,我们将学习file关键字。 实际上,file关键字用于将输出提取到指定文件。 如果您阅读了我们先前的教程Python文件操作 ,那么您应该了解基本的文件操作。

So, you have to open a file in writable mode first, then use the file pointer as the value of file keyword in print() function. See the following code to understand the python print file usage.

因此,您必须首先以可写模式打开文件,然后将文件指针用作print()函数中file关键字的值。 请参阅以下代码以了解python打印文件的用法。

# open a file in writable mood
fi = open('output.txt', 'w')

# initialize a list
initList = ['camel', 'case', 'stop']

# print each words using loop
print('Printing using default print function')
for item in initList:
    print(item, file=fi)  # use file keyword

print(file=fi)

# print each words using modified print function
print('Printing using modified print function')
for item in initList:
    print(item, end='-', file=fi)  # use file keyword

# close the file
fi.close()

And you will get the same output as the previous example in an output text file.

您将在输出文本文件中获得与前面的示例相同的输出。

That’s all about Python print. Hope that you understood it well. For any further query, feel free to use the comment section. Best of luck.

这就是关于Python打印的全部内容。 希望您理解得很好。 对于任何其他查询,请随时使用评论部分。 祝你好运。

翻译自: https://www.journaldev.com/15182/python-print

python print

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值