Python Print()函数完整用法指南及示例 您所在的位置:网站首页 python函数print怎么用 Python Print()函数完整用法指南及示例

Python Print()函数完整用法指南及示例

#Python Print()函数完整用法指南及示例| 来源: 网络整理| 查看: 265

本教程通过大量的例子和用例解释了如何使用Python的Print函数来Print变量、列表、带和不带换行的Print等。

在Python中,print()函数被用来获取输出和调试代码。这个函数用于在控制台中显示指定的消息或值。消息可以是一个字符串或任何其他对象。

我们可以说Print函数在编程中是无用的,但它实际上是最广泛使用和最强大的调试工具。调试是指发现、删除和修复代码中的错误和误差的行为。

Python Print()函数

如果代码中有些地方不对,那么我们可以使用print函数来Print代码中发生的情况。很多时候,我们期望一个变量的某个值是一个东西,但是我们不能看到我们的程序看到的东西。

如果我们使用print函数来Print出一个变量的值,那么我们就会看到我们认为在我们的程序中没有出现的东西。

Python Print()函数的语法/格式

print(*object,sep=””,end=”n”,file=sys.stdout,flush=False)

*object:一个或多个要Print的对象。 sep:对象之间的分隔符。默认值=单个空格

例子:

a=‘Welcome’ b=‘Python’ print(a,b,sep=‘,‘)

输出:

“Welcome,Python”

例子:

a=‘Welcome’ b=‘Python’ print(a,end=‘;’) print(b)

输出:

“Welcome,Python”

例子:

创建一个名为”demo.py”的文件并粘贴以下代码。

newfile=open('demo.txt','w') print('Welcome to the tutorial') newfile.close()

使用”python demo.py;output.txt”运行该程序。它将创建一个文件”output.txt”,并将Print文本加入其中。

Python Print()函数完整用法指南及示例

例子:

``` demo=open(“demo.txt”,“a”) demo.write(“Welcome!”) demo.flush() demo.write(“One more line!”) ```

Python Print()函数完整用法指南及示例

Python中print实例

print():该函数用于显示空行。

print(“strings”):当字符串被传递给该函数时,该字符串将被原样显示。

例子:print(“Hello World”),print(‘Hello World’)和print(“Hello”,”World”)

我们可以使用单引号或双引号,但要确保它们在一起。

在终端运行命令”python”,它将打开Python控制台,你可以同时检查输出结果

运行以下语句并查看输出,以了解Print函数的工作原理:

“print(“Print_Function”)” “print(‘Print_Function’)“ “print(“Print”,“Function”)”

输出:

Python Print()函数完整用法指南及示例

串联

既然我们在讨论print()函数,那么了解一下串联就很有意思了。串联的意思是将事物结合起来。

在print()函数中,我们使用”+”或”,”符号来合并两个或多个字符串,或者我们可以使用””反斜杠。这个字符被称为转义字符。它将转义字符的特征。

注意:如果我们使用”,”来合并字符串,那么,两个字符串之间会有一个空格。如果我们使用”+”符号,那么,这两个词之间就没有空格。

例1:

print( “ Welcome to the article! ”, “ Have a nice day! ” )

Python Print()函数完整用法指南及示例

例2:

print(“ Welcome to the article! ”+ “ Have a nice day! ” )

Python Print()函数完整用法指南及示例

例3:

print (“ Welcome to the article! ”) \

Python Print()函数完整用法指南及示例

Python中Print变量

字符串可以被分配给变量。例如,我们有两个字符串,分别命名为”str1″和”str2″

例1:

str1 = ‘ Welcome ’ print(str1)

Python Print()函数完整用法指南及示例

例2:

str1 = ‘ Welcome ’ str2 = ‘ Back ’ print(str1, str2)

Python Print()函数完整用法指南及示例

在Python中Print字符串

Print using as a string使用”%s”字符将变量作为Python中的一个字符串来指代。

例1:

str1 = ‘ Python ’ print(“Hey! %s” % str1)

Python Print()函数完整用法指南及示例

Print时不加换行

在Python中,如果我们想Print没有换行的语句,那么语法将是如下:

print( “ Hello ”, end= “” ) print( “ Guys! ” )

输出:

Python Print()函数完整用法指南及示例

Python带换行的Print

在Python中,如果我们想用换行来Print语句,那么语法将是:

print( “ Hello! ” ) print( “ Guys! ” )

输出

Python Print()函数完整用法指南及示例 在Python中Print列表

在Python中,列表是重复的值与它们不同位置的组合。在创建列表时,所有存在于列表中的值都可以在序列中传递。

例子:

在这个例子中,列表包含重复的值。

demolist = [ 1, 1, 2, 2, 3, 4, 5, 6, 7, 8] print(“Output: ”) print(demolist)

输出:[ 1, 1, 2, 2, 3, 4, 5, 6, 7, 8]

Print函数参数

在Python中,参数是我们在调用函数时传递给它的值。

在这个例子中,”x”和”y”是我们在加法函数中传递的两个参数。

例子:

def addition ( x, y ) print( x + y ) addition(7,8)

输出:14

它将返回我们作为参数传递的两个数字的总和。

如何在Python中Print其他数据类型 %d: 用于整数。例子:print( “ Number: %d ”, % 10 ) %e:用于指数。例子:print( “ Exponential Number: %e ”, % 10 ) %f: 用于浮点数。例子:print( “ Float Number: %f ”, % 10 ) %o:用于八进制。例子:print( “ Octal Number: %o ”, % 10 ) %x:用于十六进制。例子:print(“ Hexadecimal Number: %x ”, % 10) Python Print()函数完整用法指南及示例 更多Python中Print的例子

下面给出了在Python中使用print()函数的各种方法。

例子1:

“n”is used for Line break. print(“onentwonthreenfournfivensixnsevenneightnninenten”)

Python Print()函数完整用法指南及示例

例2:

如果我们想写一个词多次而不重复。

print( ‘ -Hello ’*5 ;numbers you want to print the word;)

Python Print()函数完整用法指南及示例

 

示例 3:

\t ”标志用于当我们想要单词中的制表符空间时,

print( “”” Names: \t1 Riya \t2 Komal “”” )

Python Print()函数完整用法指南及示例

Python print到文件

在Python中,print()函数支持”file”参数。它指定或告诉程序该函数应该在一个给定的对象中写入哪里。默认情况下,它是sys.stdout。

有两个基本的目的:

#1)print到STDERR

它将指定文件参数为sys.stderr。它主要是在调试小程序时使用。对于大型程序,建议使用调试器。

例子:

import sys print( “ Welcome ”, file = sys.stderr )

#2)print到外部文件

它将在文件参数中指定所需文件的名称,而不是默认值。

如果该文件不存在,将创建一个同名的新文件。

如果我们在调用print()命令时没有指定文件参数,那么它将在终端显示文本。

如果我们使用open命令,那么它将以写模式加载文件。当我们调用print()函数时,文本将被直接写入文件中。

例子:

# ‘ w ’ flag is used to write to the file. demo = open( ‘ demo.txt ’, ‘w’ ) print( “ Welcome ” ) demo.close()

Python Print()函数完整用法指南及示例

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有