Print Statement
In Python, the print()
function is commonly used to display output. The print()
function can be used to print text, variables, or a combination of both to the console. Here are some examples:
1)Printing Text:
print("Hello, World!")
2)Printing Variables:
name = "John"
age = 30
print("Name:", name, "Age:", age)
3)Formatting Output:
x = 10
y = 20
sum_result = x + y
print(f"The sum of {x} and {y} is {sum_result}.")
In this example, the
f
before the string allows you to embed expressions inside string literals, using curly braces
{}
to enclose the expressions.4)Multiple Arguments in print()
:
a = 5
b = 3
c = a * b
print("Value of a:", a, "Value of b:", b, "Result of a * b:", c)
The
print()
function can take multiple arguments separated by commas.These are basic examples, and there are more advanced formatting options and techniques available in Python for printing. If you're working with Python 3.6 or later, f-strings (formatted string literals) are a concise and powerful way to format output.
Syntax:
print(value,sep=' ',end=' ',file=sys.stdout, flush=False)
Print Parameters
1)Objects to Print:
x = 10
y = 20
print(x, y)
Note:
You can pass multiple objects (variables, values, etc.) as arguments to
print()
.2)Separator (sep
parameter):
name = "John
age = 30
print(name, age, sep=", ")
Note:
The
sep
parameter allows you to specify a separator between the objects. The default is a space.3)End (end
parameter):
print("This is the end", end="!!!\n")
Note:
The end
parameter allows you to specify what to print at the end. By default, it's a newline character (\n
).
4)File (file
parameter):
with open("output.txt", "w") as f:
print("Writing to a file", file=f)
Note:
The
file
parameter allows you to specify a file-like object where the output will be written. In this example, it writes to a file instead of the console. 😊😊😊