Python displays exactly the text found between the parentheses. This simple example introduces the built-in print function, which is used to display information to the terminal.Let’s break down the anatomy of the code:
Copy
Ask AI
print("Hello future Python programmer!")
The code consists of:
• The function name: print
• An opening parenthesis: (
• A text string delimited by double quotes: “Hello future Python programmer!”
• A closing parenthesis: )By combining these elements, Python understands that it should display the provided string on the terminal.
The print function is built into Python and requires no additional imports. Beyond printing text, functions in Python can process values, produce side effects, and even return data, allowing you to design your own functions for more complex tasks.
Let’s revisit the function’s structure with another example:
Copy
Ask AI
print("Hello future Python programmer!")
In this example, a single string argument is passed to the print function. In Python, any text enclosed in quotes is treated as a string, ensuring that the text is interpreted as data rather than code.If you pass another string, Python will print that string as well. For example:
Copy
Ask AI
>>> print("Hello future Python programmer!")Hello future Python programmer!>>> print("Python is a great language")Python is a great language
Python verifies the print function, confirms that a valid string argument has been provided, executes the internal code, and then returns control to your program.
To print multiple lines, you can either call the print function several times—each on its own line—or include the newline character (\n) within your string. For example:
You can also print multiple arguments on the same line. When several arguments are passed (separated by commas), Python concatenates them with a space by default:
Python’s print function supports keyword arguments that let you fine-tune its output. Two common keyword arguments are:• end – Specifies what to print at the end of the output. The default is a newline character.
• sep – Specifies the separator between multiple arguments. The default is a space.For example, to override the default newline behavior:
Copy
Ask AI
>>> print("Hello!", end="")... print("Python is a great language")Hello!Python is a great language
You can also use different values for the end parameter:
Copy
Ask AI
>>> print("Hello!", end="!")... print("Python is a great language")Hello!!Python is a great language>>> print("Hello!", end="❤️")... print("Python is a great language")Hello!❤️Python is a great language
The sep keyword can be used to change the default spacing between arguments:
The print function is an essential built-in Python tool for displaying output on the console. By calling this function with parentheses and providing values—commonly strings enclosed in quotes—Python knows to output that content. In this article, we learned:• The basics of the print function and its syntax.
• How arguments (such as strings) are passed to the function.
• The role of special characters like the newline (\n).
• How keyword arguments like end and sep allow for versatile output control.
This article has provided a comprehensive overview of the print function. Practice what you’ve learned by experimenting with these examples in your own Python environment!