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
stopPrinting 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 keywordprint(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

python print_Python print()相关推荐

  1. python中print()换行的问题

    title: python中print()换行的问题 date: 2022-04-17 14:36:12 tags: - python使用 - python学习 categories: python ...

  2. python中print()函数的功能与格式

     print单纯输python学习之变量类型中的十种数据类型只需要用print()函数即可,()里面直接写变量名. 下面重点介绍print格式输出: 第一种方法:一个萝卜一个坑,下面的代码中,{0}. ...

  3. python程序输出田字格_Python用print()函数输出田字格

    展开全部 Python用2113print()函5261数输出田字格示例代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- # Python用pri ...

  4. Python入门之print()函数中sep和end的用法

    一.print()函数 用于打印输出 刚刚做了一个作业,比如说,你要用print语句输入这样的内容 要求是, 1.向外界获取生日的人的名字 2.定义一个变量name存储这个名字 3.最后得出,&quo ...

  5. Swift:print()vs println()vs NSLog()

    本文翻译自:Swift: print() vs println() vs NSLog() What's the difference between print , NSLog and println ...

  6. Python中的“ @”(@)符号有什么作用?

    我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...

  7. 如何使用Python numpy.where()方法

    In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...

  8. Python字符串title()

    Python String title() Python字符串title() Python String title() function returns a title cased version ...

  9. Python字符串isdecimal()

    Python String isdecimal() function returns True if all the characters in the string are decimal char ...

最新文章

  1. Python 可视化近 90 天的百度搜索指数 + 词云图
  2. XManager 远程连接Netbackup图形用户界面
  3. windows下升级nodejs版本
  4. django配置在MySQL_怎么在Django中安装与配置mysql
  5. ae2021最新遮罩路径图形扭曲插件:BAO Boa for Mac
  6. SAP License:如何更好的管理ERP中的供应商?
  7. 万能模拟器eve-ng介绍
  8. python学习(四)----函数
  9. 服务器slot槽位位置,华为OSN2500子架及其槽位的分配
  10. ORACLE--comment 通过表注释/通过注释查询表名
  11. 数学在线绘图计算机,Desmos Graphing Calculator插件,在线可视化图形计算器
  12. 计算机主机结构图手画,流程图怎么画简单又漂亮
  13. 如何编写一个短线交易策略(收藏)
  14. FaaS(功能即服务)
  15. 高级渗透之VBS调用WMI接口
  16. 春节流量争夺战:互联网巨头跪求你收红包
  17. 往DAO类中注入@PersistenceContext和@Resource的区别
  18. director(director)
  19. STM32F1基于H桥的电机控制程序分析
  20. 小菜鸟之oracle数据字典

热门文章

  1. Recommender Systems协同过滤
  2. Go语言中的字符和字符串
  3. JAVA 内存泄露的理解
  4. SDL_BlitSurface的参数是两个PNG时,如何保护其透明度
  5. [转载] c++list遍历_List、Set、数据结构、Collections
  6. [转载] Python数据可视化库-Matplotlib——折线图绘制
  7. [转载] python之numpy的基本使用
  8. Linux编程获取本机IP地址
  9. 二维平面上判断点是否在三角形内
  10. oracle 查看最大连接数与当前连接数