If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Python,
print()
is a built-in function used to display output on the console or terminal. It allows you to print text, variables, and other data to the screen, making it a fundamental tool for debugging and displaying information to users.The syntax of the
print()
function is as follows:pythonCopy code
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Here's what each argument means:
objects
: One or more objects (variables, strings, numbers, etc.) that you want to print. You can pass multiple objects separated by commas, andprint()
will display them with a space between each object by default.sep
: Specifies the separator between the objects when printing multiple objects. The default separator is a space character' '
.end
: Specifies the string to be appended after all the objects have been printed. The default is a newline character'\n'
, which means eachprint()
call ends with a new line. You can change it to any string you want.file
: Specifies the file-like object where the output should be written. By default, it writes to the standard output, which is the console.flush
: IfTrue
, the output is forcibly flushed. Flushing means the output is written immediately to the file or terminal rather than being buffered. The default isFalse
.Examples:
- Printing a simple message:
pythonCopy code
print("Hello, World!") # Output: Hello, World!
- Printing multiple objects with a custom separator and ending:
pythonCopy code
name = "Alice" age = 25 print("Name:", name, "Age:", age, sep=" - ", end=" years old\n") # Output: Name: Alice - Age: 25 years old
- Printing numbers with formatting:
pythonCopy code
x = 3.14159 y = 42 print(f"x = {x:.2f}, y = {y:04d}") # Output: x = 3.14, y = 0042
The
print()
function is highly versatile and can be used to print various types of data. It is often used for debugging purposes, displaying user messages, formatting output, and more. You can customize its behavior using the optional arguments as needed.
Comments: 0