In Python, the primary way to output data to the console or screen is by using the built-in print() function. It's one of the first functions any Python programmer learns.
You can pass a string, number, or any other object to the print() function to display it.
print("Hello, World!")
print(123)
You can pass multiple items to the print() function, separated by commas. By default, it will separate each item with a single space.
x = "John"
y = 25
print("My name is", x, "and I am", y, "years old.")
sep and endThe print() function has optional parameters that give you more control over the output.
sep (separator): Specifies how to separate the objects, if there is more than one. The default is a space ' '.end (end): Specifies what to print at the end. The default is a newline character '\n'.
// Using a custom separator
print("apple", "banana", "cherry", sep="---")
// Printing without a newline at the end
print("Hello", end=" ")
print("World!")
Introduced in Python 3.6, f-Strings provide a concise and convenient way to embed expressions inside string literals for formatting.
name = "Jane"
age = 30
print(f"Her name is {name} and she is {age} years old.")
Which parameter of the `print()` function do you use to specify the character that separates multiple items?