Lesson · 40 min · Free
Python Print & Output
Python Print & Output body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto; } code { font-famil
Python Programming - Basics
Python Print & Output
In the realm of pharmacy and biotechnology, data communication is paramount. Whether you're displaying drug efficacy results, patient demographics, or the output of a bioinformatics script, the ability to present information clearly is fundamental. In Python, the primary mechanism for displaying output to the console is the built-in print() function. This lesson will delve into the versatility of print() , demonstrating how it can be used to output various data types and format information for readability. The print() function is incredibly straightforward in its basic usage. You simply pass the data you wish to display as an argument within its parentheses. Python will then convert this data into a string representation and output it to the standard output stream, which is typically your console or terminal. It's a foundational tool for debugging, providing user feedback, and presenting the final results of your scripts. Consider a scenario where you've calculated a drug's half-life or the concentration of a compound. Using print() , you can display these values directly. You can also combine multiple items within a single print() call, separated by commas. By default, these items will be separated by a space.
Basic Usage of print()
# Printing a simple string print("Hello, Pharmacy World!") # Printing a numerical value drug_concentration = 15.75 print(drug_concentration) # Printing multiple items, separated by spaces by default drug_name = "Paracetamol" dosage_mg = 500 print("Drug:", drug_name, "Dosage:", dosage_mg, "mg") # Printing the result of an expression result = 12 * 5.5 print("Calculation result:", result) Beyond simple output, the print() function offers parameters to control the separation between items and the character appended at the end of the output. The sep parameter allows you to specify a custom separator string, replacing the default space. The end parameter, by default a newline character ( \n ), dictates what is printed after all items have been displayed. This is particularly useful when you want to keep subsequent print statements on the same line or introduce a specific terminator.
Controlling Output with sep and end
# Using 'sep' to change the separator patient_id = "P101" visit_date = "2023-10-26" print("Patient ID", patient_id, "Visit Date", visit_date, sep=" | ") # Using 'end' to prevent a newline print("Processing batch 1...", end="") print(" Done.") # This will appear on the same line as "Processing batch 1..." # Combining 'sep' and 'end' compound_elements = ["C", "H", "O", "N"] print("Compound elements:", *compound_elements, sep="-", end=".\n") Understanding how to effectively use print() is crucial for any Python programmer, especially in scientific fields where clear data presentation is paramount. It allows you to monitor the execution of your code, inspect variable values, and present your findings in an understandable format.
Key Takeaways
The print() function is Python's primary tool for displaying output to the console. It can output various data types (strings, numbers, variables) and expressions. Multiple items passed to print() are separated by a space by default. The sep parameter allows you to specify a custom separator string between items. The end parameter controls the character appended at the end of the output, defaulting to a newline ( \n ).
Practice Exercise
Imagine you are analyzing the results of a drug trial. You have recorded the patient_age as 62 , the drug_dosage_mg as 250 , and the response_rate_percent as 87.5 . Write a Python script using a single print() statement that outputs this information in a clear, human-readable format. Ensure that the output for each piece of information is separated by a semicolon and a space ( ; ), and that the entire line ends with the phrase "Trial Data." (including the period).
Watch the full lesson — free
This topic is part of Python Programming - Basics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →