Python Coding Day 1 | The print() Function and Comments
Mastering Python Basics: The print() Function and Comments
Watch the lesson tutorial 🔻
print() Function and CommentsThe Python print() function is often the very first tool any developer learns, as it provides instant feedback and is essential for debugging and displaying information. Alongside this, comments are crucial for writing clear, maintainable code.
This article breaks down the foundational uses of the print() function and the purpose of comments in Python.
1. Simple Output: The Classic Greeting
Title: Printing a Single String
Brief Definition
This is the most fundamental use of the print() function. It takes a single string of text enclosed in quotes and displays it directly to the console.
Practical Python Code
print("Hello World!")
Code Description
print(): This is the built-in function that instructs Python to output data.("..."): Everything inside the parentheses is the argument passed to the function."Hello World!": This is a string literal (text data) enclosed in double quotes. The function prints this exact sequence of characters.Implicit Newline: After printing the string, the
print()function automatically adds a newline character at the end, moving the cursor to the next line for future output.
2. Line Breaks with the Newline Character
Title: Controlling Lines with \n
Brief Definition
The backslash followed by the letter 'n' (\n) is an escape sequence that represents a line break. It allows you to print text across multiple lines using a single print() call.
Practical Python Code
print("Hello World!\nHello World!")
Code Description
Single String: The entire output is still treated as one string argument.
\nInsertion: When Python processes the string, it interprets the\nnot as literal text, but as a command to start a new line.Result: The first "Hello World!" is printed, a line break occurs, and then the second "Hello World!" is printed on the line immediately below it.
3. Combining Strings (Concatenation)
Title: Concatenating Strings with the + Operator
Brief Definition
String concatenation is the process of joining two or more strings end-to-end to create a new, single string. This is achieved using the addition operator (+).
Practical Python Code (Combined Examples)
# A. Explicitly adding a space string
print("Hello" + " " + "World!")
# B. Printing without any space
print("Hello" + "World!")
# C. Including the space within one of the strings
print("Hello " + "World!")
Code Description
The
+Operator: When used between two strings,+glues the strings together.Example A: To get a space between "Hello" and "World!", we must explicitly add a third string:
" ". The code evaluates to"Hello World!".Example B: Since no space string is added, the result is
"HelloWorld!". This highlights that+does not automatically insert spaces like some other print methods (e.g., separating values with a comma).Example C: The space is included directly as the last character of the first string,
"Hello ", achieving the same visual result as Example A.
4. Documentation and Ignored Code
Title: The Comment Symbol (#)
Brief Definition
A comment is a line of code that the Python interpreter completely ignores. It is used by programmers to document, explain, or temporarily disable (comment out) parts of the code.
Practical Python Code
# This is a comment. It explains the next line of code.
variable = 10 # This is an inline comment explaining the variable's value
print(variable)
- by Chirana Nimnaka

Comments
Post a Comment