在 Python 中换行输出变量的方法:使用转义字符 "\n" 在字符串中表示换行。指定 print() 函数的 end 参数为 "\n"。将字符串包裹在多行引号中,使用三个单引号或三个双引号。

如何在 Python 中换行输出变量
在 Python 中,使用 print() 函数输出变量。要换行输出变量,可以使用以下方法:
1. 转义字符
使用转义字符 \n(换行符)在字符串中表示换行。
立即学习“Python免费学习笔记(深入)”;
<code class="python">print("变量 1\n变量 2")</code>2. print() 函数中的 end 参数
指定 print() 函数的 end 参数为 "\n"。
<code class="python">print("变量 1", end="\n")
print("变量 2")</code>3. 多行字符串
使用三个单引号或三个双引号将字符串包裹在多行中。
<code class="python">variable = """ 变量 1 变量 2 """ print(variable)</code>
示例
以下示例演示了这些方法:
<code class="python"># 使用转义字符
print("变量 1\n变量 2")
# 使用 print() 函数的 end 参数
print("变量 1", end="\n")
print("变量 2")
# 使用多行字符串
variable = """
变量 1
变量 2
"""
print(variable)</code>输出:
<code>变量 1 变量 2 变量 1 变量 2 变量 1 变量 2</code>
以上就是python如何换行输出变量的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号