Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python的print用法

作者:网络神话—爷恋   发布日期:2026-01-30   浏览:103

# 示例代码:使用 print 函数输出不同类型的变量

# 输出字符串
print("Hello, World!")  # 输出: Hello, World!

# 输出整数
number = 42
print("The answer is", number)  # 输出: The answer is 42

# 输出浮点数
pi = 3.14159
print("Pi is approximately", pi)  # 输出: Pi is approximately 3.14159

# 使用逗号分隔多个值
name = "Alice"
age = 30
print("Name:", name, "Age:", age)  # 输出: Name: Alice Age: 30

# 使用 + 操作符连接字符串(注意需要转换非字符串类型)
print("Name: " + name + " Age: " + str(age))  # 输出: Name: Alice Age: 30

# 使用格式化字符串 (f-string)
print(f"Name: {name}, Age: {age}")  # 输出: Name: Alice, Age: 30

# 输出空行
print()  # 输出一个空行

# 输出带换行符的多行字符串
multi_line_string = """This is a 
multi-line string."""
print(multi_line_string)

# 输出不换行,末尾加其他字符
print("No newline here", end=" ")  # 输出后不换行,默认是换行符 \n
print("Continuing on the same line")  # 输出在同一行继续

# 输出到文件(假设文件名为 output.txt)
with open("output.txt", "w") as file:
    print("Writing to a file", file=file)

解释说明:

  • print() 函数用于将指定的内容输出到控制台。
  • 可以输出单个或多个值,值之间用逗号分隔。
  • 使用 + 操作符可以连接字符串,但需要注意类型转换。
  • 使用 f-string (f"...") 可以方便地插入变量。
  • end 参数可以指定输出结束时的字符,默认是换行符 \n
  • file 参数可以指定输出的目标文件。

上一篇:python定义类

下一篇:python连接oracle数据库

大家都在看

python时间格式

pythoneval函数用法

python读取文件路径

staticmethod在python中有

python 保存json文件

python开发windows应用程序

python中len是什么意思

python 私有函数

python ord和chr

python中的yield

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站