python如何换行输出一句话

admin 阅读:45 2024-07-27
在 python 中实现一句话换行输出有两种方法:一是使用转义字符 "n",二是使用字符串内嵌多行文本,例如:print("hellonworld") 或 print("""helloworld""")。

python如何换行输出一句话

Python 中换行输出一句话

在 Python 中,有两种方法可以在输出同一句话时实现换行:

1. 使用转义字符 'n'

转义字符 'n' 代表换行符。要换行输出一句话,可以在字符串中使用 'n':

立即学习Python免费学习笔记(深入)”;

print("HellonWorld")

输出:

Hello
World

2. 使用字符串内嵌多行文本

Python 允许使用三重引号 (""") 来表示字符串,其中可以包含多行文本。要换行输出一句话,可以使用三重引号并将换行符直接包含在字符串中:

print("""Hello
World""")

输出:

Hello
World

示例:

# 使用转义字符输出换行信息
message = "**Current Status**nn" 
          "System: Runningn" 
          "Network: Connectedn" 
          "CPU Usage: 30%"

print(message)

# 使用字符串内嵌输出换行信息
message = """**Current Status**

System: Running
Network: Connected
CPU Usage: 30%"""

print(message)

输出:

**Current Status**

System: Running
Network: Connected
CPU Usage: 30%

**Current Status**

System: Running
Network: Connected
CPU Usage: 30%
声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!