print() Is More Than Hello World



Start NorthStar

Python Learning Notes

print() Is More Than Hello World

A beginner-friendly Python note on using sep and end to control output formatting.

Python 学习笔记

print() 不只是打印一句话

一篇适合初学者的 Python 笔记,讲清楚如何用 sep 和 end 控制输出格式。

Share this article

Copy the link, open your phone share sheet, scan the QR code for WeChat, or send by email.




Email

Many beginners first meet print() through print("Hello World"). That is a good starting point, but print() is more than a way to print one sentence. It is also one of the most useful tools for formatting output and debugging beginner programs.

This note focuses on two practical parameters: sep and end. Once you understand them, you can control what goes between printed values and what happens at the end of a printed line.

Common Mistake

Beginners often try to build formatted output with string concatenation:

print("CP" + " - " + "Club" + " - " + "Python")

This works, but it gets messy quickly. print() can already receive multiple values:

print("CP", "Club", "Python")
CP Club Python

By default, Python puts one space between the values.

Core Idea

Two print() parameters are especially useful for beginners:

  • sep: short for separator. It controls what goes between multiple values.
  • end: it controls what comes after this print statement finishes.

Remember this sentence:

sep controls the middle. end controls the ending.

Recommended Pattern: Use sep for the Middle

If you want the values to be separated by - instead of spaces, write:

print("CP", "Club", "Python", sep=" - ")
CP - Club - Python

Here, sep=" - " means: put - between each pair of printed values.

Another example:

print("2026", "07", "30", sep="/")
2026/07/30

Recommended Pattern: Use end for the Ending

By default, every print() ends with a new line:

print("Hello")
print("Python")
Hello
Python

If you want the next output to continue on the same line, set end to a space:

print("Hello", end=" ")
print("Python")
Hello Python

Here, end=" " means: after printing Hello, do not move to a new line; add a space instead.

Comparison

Code Good for Watch out
print("A", "B") Printing multiple values The default separator is one space
print("A", "B", sep="-") Controlling the separator sep only controls the middle
print("A", end=" ") Controlling what happens after the line end affects where the next output appears

Why This Matters

In CCC and Python exercises, output format matters. One extra space or one missing newline can make the output look wrong.

Also, print() is one of the most useful debugging tools for beginners:

score = 87
print("score =", score)
score = 87

When you understand sep and end, your debugging output becomes much easier to read.

Practice

  1. Use sep="/" to output 2026/07/30.
  2. Use two print() calls to output one line: Hello Python.
  3. Challenge: output A -> B -> C without using string concatenation.

One-Sentence Takeaway

sep controls what goes between values; end controls what comes after the printed line.

很多同学第一次见到 print(),只会想到 print("Hello World")。这当然是最基础的用法,但如果你只把 print() 当成“打印一句话”,后面调试程序和整理输出时会很不方便。

这一篇只讲两个很实用的参数:sepend。记住它们以后,你就能控制输出内容中间放什么,以及一行输出结束后接什么。

常见错误

新手常常为了拼出一行输出,写很多字符串和空格:

print("CP" + " - " + "Club" + " - " + "Python")

这段代码可以运行,但一旦内容变多,就容易看乱。其实 print() 本来就可以一次接收多个内容:

print("CP", "Club", "Python")
CP Club Python

默认情况下,多个内容中间会自动用一个空格隔开。

核心概念

print() 有两个非常适合初学者先掌握的参数:

  • sep:separator,控制多个内容中间放什么。
  • end:控制这一行输出结束后接什么。

可以先记一句话:

sep 管中间,end 管结尾。

推荐写法:用 sep 控制中间

如果你希望多个内容中间不是空格,而是 - ,可以这样写:

print("CP", "Club", "Python", sep=" - ")
CP - Club - Python

这里 sep=" - " 的意思是:每两个输出内容中间,都放一个 -

再比如:

print("2026", "07", "30", sep="/")
2026/07/30

推荐写法:用 end 控制结尾

默认情况下,每次 print() 结束后都会换行:

print("Hello")
print("Python")
Hello
Python

如果你想让下一次输出接在同一行,可以把 end 改成空格:

print("Hello", end=" ")
print("Python")
Hello Python

这里 end=" " 的意思是:第一行输出结束后,不换行,而是接一个空格。

对比一下

写法 适合情况 注意点
print("A", "B") 默认输出多个内容 中间默认是一个空格
print("A", "B", sep="-") 控制多个内容之间的分隔符 sep 只管中间
print("A", end=" ") 控制这一行输出结束后接什么 end 会影响下一次输出的位置

为什么这个概念很重要?

在 CCC 和 Python 练习里,输出格式经常很严格。多一个空格、少一个换行,都可能让答案看起来不对。

更重要的是,print() 是新手最实用的调试工具。你可以临时输出变量:

score = 87
print("score =", score)
score = 87

如果再配合 sepend,你就能更清楚地观察程序运行过程。

小练习

  1. sep="/" 输出 2026/07/30
  2. 用两次 print() 输出同一行:Hello Python
  3. 挑战题:输出 A -> B -> C,不要用字符串加号拼接。

一句话记住

sep 管多个内容中间放什么,end 管这一行结束后接什么。

官网 CTA

想继续学 Python 新手最常用的基础工具,可以看下一篇 Python Learning Notes:变量不是盒子,更像标签。

Questions or feedback?

Have a question about this article or want to suggest an improvement? Send us a private message.

Send Feedback

有问题或建议?

如果你对本文有疑问,或希望提出改进建议,请给我们发送私密留言。

发送反馈

Back to Python Learning Notes
返回 Python 学习笔记

Scroll to Top