print函数是Python中用于输出内容的函数,可以输出字符串、变量、表达式等内容。格式化字符串是指在输出字符串中插入变量或表达式的值,使输出更加清晰和易读。
下面是一些示例代码,演示如何使用print函数和格式化字符串:
# 使用print输出字符串print("Hello, World!")# 使用print输出变量的值name = "Alice"age = 30print("My name is", name, "and I am", age, "years old.")# 使用格式化字符串输出变量的值name = "Bob"age = 25print(f"My name is {name} and I am {age} years old.")# 使用格式化字符串输出表达式的值x = 10y = 5print(f"The sum of {x} and {y} is {x + y}.")在Python3.6及以上版本中,可以使用f字符串来进行格式化输出,通过在字符串前加上"f"或"F",然后在字符串中使用大括号{}来插入变量或表达式的值。这样可以使代码更加简洁和易读。


