PYTHON 函数和模块
PYTHON 条件控制 

PYTHON 输入输出

Python 是一种广泛使用的高级编程语言,它以其清晰的语法和代码可读性而闻名。Python 的输入和输出操作非常直观,下面是一些基本的输入输出操作示例:


输入(Input)

从用户那里获取输入:

user_input = input("请输入一些内容:")
print("你输入的内容是:", user_input)

输出(Output)

输出到控制台通常使用 print() 函数。print() 函数可以接受多个参数,并将它们以空格分隔的形式输出到控制台。

print("Hello, World!")


格式化输出:

用 % 操作符:

name = "world"
print("Hello, %s!" % name)


使用 str.format() 方法:

name = "world"
print("Hello, {}!".format(name))


使用 f-string(Python 3.6+):

name = "world"
print(f"Hello, {name}!")


文件输入输出

写入文件:

with open('example.txt', 'w') as file:
    file.write("这是一些文本数据。")


读取文件:

with open('example.txt', 'r') as file:
    content = file.read()
    print(content)


逐行读取文件:

with open('example.txt', 'r') as file:
    for line in file:
        print(line, end='')  # end='' 防止print函数添加额外的换行符


这些是 Python 中一些基本的输入输出操作。