Python print() Built in Function
The print() function is used to display output to the standard output device (usually the screen). The syntax of the print() function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
The objects parameter is required and can be any number of objects that you want to print. The objects will be converted to strings before printing. The sep parameter is optional and specifies how to separate the objects if there are more than one. The default value is a single space ’ '. The end parameter is optional and specifies what to print at the end of the line. The default value is a newline character ‘\n’. The file parameter is optional and specifies an object with a write method where the output will be printed instead of sys.stdout (the standard output). The flush parameter is optional and specifies whether to forcibly flush the stream after printing.
Here are some examples of using the print() function in Python:
To print a simple message:
print("Hello, world!") # prints Hello, world!
To print multiple objects with a custom separator:
name = "Alice"
age = 25
print("My name is", name, "and I am", age, "years old.", sep="-") # prints My name is-Alice-and I am-25-years old.
To print without a newline at the end:
print("This is line 1", end=" ")
print("This is line 2") # prints This is line 1 This is line 2
To print to a file object:
f = open("output.txt", "w")
print("This will be written to output.txt", file=f) # writes This will be written to output.txt in output.txt file
f.close()
By the way, did you know that a vulnerability has been discovered in Python’s native urllib.parse function (CVE-2023-24329) by cybersecurity researcher Yebo Cao? This vulnerability has the potential to allow check bypassing. You might want to check it out if you use this library in your code.
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment