在Python中,打印输出可以通过`print()`函数实现。以下是一些基本的使用方法:
1. 打印字符串:
```python
print("Hello, World!")
2. 打印变量的值:
```python
x = 10
print(x)
3. 打印多个变量的值:
```python
x = 10
y = 5
print(x, y)
4. 打印表达式的结果:
```python
x = 10
y = 5
print(x + y)
5. 格式化输出:
```python
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
6. 使用`sep`和`end`参数控制输出的分隔符和结尾字符:
```python
print("Hello", "World!", sep=", ", end="!\n")
7. 使用文本处理库(如`reportlab`)生成打印报告:
```python
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
def generate_report(filename, title, content):
doc = SimpleDocTemplate(filename, pagesize=letter)
styles = getSampleStyleSheet()
title_paragraph = Paragraph(title, styles['Title'])
text_paragraph = Paragraph(content, styles['Normal'])
content = [title_paragraph, Spacer(1, 12), text_paragraph]
doc.build(content)
generate_report("report.pdf", "打印报告示例", "这是报告的内容")
8. 打印列表:
```python
info = ["Tom", "BeiJing", "HouHai"]
print(*info, sep="_")
9. 打印图形:
```python
width = 10
height = 5
for i in range(height):
print("* " * width)
10. 打印对象:
```python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person = Person("Bob", 25)
print(person.__dict__)
以上是Python中打印输出的基本方法。您可以根据需要选择不同的打印技巧和格式化选项来满足您的打印需求