Python Print() 语句:如何使用示例进行打印

⚡ 智能摘要

Python print() function displays a message or object on the screen after converting it to a string. The examples below show how to print strings, blank lines, and multiple items, and how to control line endings.

  • 🖨️ 目的: The print() function outputs strings and objects to the screen.
  • 🔤 语法: Call print(object(s)) with the value you want to display.
  • 🔁 Multiple Prints: Each print() call outputs on its own new line by default.
  • Blank Lines: Use “\n” or multiply it to print empty lines.
  • ➡️ end Parameter: Change the line ending, for example end=’ ‘, in Python 3.
  • ⚠️ 版本说明: Python 3 uses print() as a function; Python 2 used it as a statement.

Python Print() Statement

Python print()函数

print() 函数 Python 用于在屏幕上打印指定消息。print 命令 Python 在屏幕上打印时打印字符串或转换为字符串的对象。

语法:

print(object(s))

如何在 Python?

通常你需要 打印字符串 在您的编码构造中。

以下是如何打印语句 Python 3:

例如:1

打印欢迎信息 Guru99,使用 Python 打印语句如下:

print ("Welcome to Guru99")

输出:

歡迎來到 Guru99

In Python 2,同样的例子如下

print "Welcome to Guru99"

例如2:

如果要打印五个国家的名字,可以这样写:

print("USA")
print("Canada")
print("Germany")
print("France")
print("Japan")

输出:

USA
Canada
Germany
France
Japan

如何打印空白行

有时你需要在 Python 程序。以下是使用以下命令执行此任务的示例 Python 打印格式。

计费示例:

让我们打印 8 个空白行。您可以输入:

print (8 * "\n")

要么:

print ("\n\n\n\n\n\n\n\n\n")

这是代码

print ("Welcome to Guru99")
print (8 * "\n")
print ("Welcome to Guru99")

输出

Welcome to Guru99







Welcome to Guru99

打印结束命令

By default, the print function in Python ends with a newline. This function comes with a parameter called ‘end’. The default value of this parameter is ‘\n’, i.e., the new line character. You can end a print statement with any character or string using this parameter. This is available only in Python 3 +。

例如1:

print("Welcome to", end=' ')
print("Guru99", end='!')

输出:

歡迎來到 Guru99!

例如2:

# ends the output with '@'
print("Python", end='@')

输出:

Python@

常见问题

In Python 2, print was a statement written as print “text”. In Python 3, print is a function called as print(“text”) with parentheses. Python 3 is the current standard, so always use the function form.

The sep parameter sets the separator placed between multiple items in one print() call. By default it is a space. For example, print(“a”, “b”, sep=”-“) outputs a-b instead of a b.

Use an f-string, such as print(f”Hello {name}”), or separate items with commas, such as print(“Hello”, name). F-strings are the modern, readable way to mix text and variables in Python 3.6年及以后。

Yes. AI assistants can spot missing parentheses, wrong quotes, or Python 2 syntax, and explain the exact error. They also suggest cleaner ways to format output, which helps beginners fix print-related mistakes quickly.

print() is a simple way to inspect data, model outputs, and intermediate values while building AI programs. Developers use it to trace how data flows through code, though logging libraries are preferred for larger projects.

总结一下这篇文章: