The print function is one of the most commonly used built-in functions in Python and is used to output the results of text, variables, or expressions in the console. It has a variety of usages and can flexibly output a variety of information.
This article will introduce the usage of the print function in detail and demonstrate its practical application with examples. Autumn and Winter Check-in Challenge
Grammar. The basic syntax of the print function is as follows:
print(*objects, sep=' ', end='', file=sys.stdout, flush=false)
Parameter description. The function parameters are explained as follows:
objects: The content to be output, which can be strings, variables, expressions, etc. Multiple parameters are separated by spaces. SEP: An optional parameter that specifies the separator between parameters. Defaults to a space. end: an optional parameter that specifies the end character after the output. The default is line breaks''。file: An optional parameter that specifies the output destination. The default is sysstdout, which indicates output to the console. flush: An optional parameter, if true, the output is immediately refreshed (i.e., it is not cached). Usage examples.
Output string.
print("hello, world!")
Output variable values.
name = "alice" age = 25 print("my name is", name, "and i am", age, "years old.")
Format the output.
name = "bob" age = 30 print("my name is %s and i am %d years old." % name, age))
Use sep parameters to separate multiple parameters.
fruits = ["apple", "banana", "cherry"] print("i like", fruits[0], "and", fruits[1], "and", fruits[2], ".")
Use the end parameter to control the end character after the output.
print("hello, world!", end="!!")
Summary. By mastering the use of the print function, we can output a variety of information more flexibly.
I hope this article will help you understand the print function in Python.