Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
So far, we have learned how to create variables and store values in them.
But how do we show those values to the user?
Python provides a built-in function called:
The print() function displays information in the program's output.
For example:
Output:
In this lesson, we will learn how to use print() with:
sependThe simplest way to display a variable is to pass it to print().
Output:
Another example:
Output:
You can print different types of values:
Output:
print() can display strings, numbers, Boolean values, variables, and many other Python objects.
You can give print() multiple values separated by commas.
Output:
Python automatically places a space between the values.
For example:
Output:
When you use commas inside print():
Python prints the values with a space between them by default.
One of the easiest ways to print different types of values is to use commas.
Output:
You can also put everything in one print():
Output:
This works even when the values have different data types.
+ OperatorYou can also join strings using the + operator.
This is called string concatenation.
Output:
The spaces are important.
Without them:
Output:
So:
means:
You cannot directly concatenate a string and a number using +.
For example:
This produces a TypeError.
Why?
Because:
is a string, while:
is an integer.
Python does not automatically convert the integer into a string when using +.
You could convert it manually:
Output:
But there is an easier and cleaner approach for formatted output:
We will learn them shortly.
+For beginners, these two approaches are worth comparing.
This is simple and automatically handles different data types.
+This works because both sides are strings.
But with numbers:
❌ This produces an error.
When you are just learning print(), using commas is often the easiest way to display multiple values.
Later, you'll learn f-strings, which are usually cleaner for formatted messages.
sep ParameterBy default, Python separates multiple items in print() with a single space.
For example:
Output:
The separator is controlled by:
The default is:
You can choose your own separator.
Output:
Another example:
Output:
You can also create a time-like format:
Output:
sep controls what appears between multiple values.
end ParameterNormally, print() moves the cursor to the next line after displaying something.
For example:
Output:
Why?
Because the default value of end is:
\n means new line.
endYou can change what print() puts at the end.
For example:
Output:
The first print() does not move to a new line.
end with a MessageOutput:
Another example:
Output:
sep → controls the space between itemsend → controls what comes after the printsep and end TogetherYou can use both parameters in the same print().
Output:
Here:
controls the spaces between the values.
And:
controls the ending.
The end parameter is useful in loops.
For example:
Output:
Without end=" ":
Output:
This is one of the most useful examples of end.
When you need to create a sentence using variables, f-strings are one of the easiest options.
An f-string starts with the letter:
before the string.
Example:
Output:
The variables are placed inside {}.
Compare these approaches.
+This works, but it becomes difficult to read as the sentence gets longer.
Much easier to read.
When you want to put variables inside a sentence, prefer an f-string.
f-strings can also control how numbers are displayed.
Suppose:
If we write:
Output:
Here:
means:
For large numbers, commas can make the output easier to read.
Output:
Here:
adds thousands separators.
And:
keeps two decimal places.
Suppose you have a roll number:
You may want to display it as:
You can use:
Output:
Here:
means:
Output:
This is much closer to the type of output used in real programs.
print() StructureAt a beginner level, you can remember this simplified structure:
The important parts are:
| Part | Purpose | Default |
|---|---|---|
| Values | Things to print | — |
sep | Separates multiple values | " " |
end | Added after printing | "\n" |
For now, you don't need to worry about advanced parameters such as file or flush.
Python's full print() signature also includes file and flush. These are useful in more advanced programs, but they are not necessary for beginners.
| Practice | ✅ Do | ❌ Don't |
|---|---|---|
| Multiple values | print("Age:", age) | Convert everything to strings unnecessarily |
| Sentences with variables | f"Hello {name}" | Use many + operators |
| Separator | sep="-" | Manually add separators everywhere |
| Same-line output | end=" " | Add many empty print() statements |
| Decimal formatting | :.2f | Show unnecessary decimal digits |
| Large numbers | :,.2f | Print difficult-to-read raw numbers |
Imagine a student result program.
Output:
We are already using the same concepts that are used in larger applications:
print()Remember these five ideas:
sep and end?A. sep="," and end=" " B. sep=" " and end="\n" C. sep="" and end="" D. sep="\t" and end="\r"
A. Delhi Mumbai Kolkata B. Delhi -> Mumbai -> Kolkata C. Delhi -> Mumbai -> Kolkata -> D. Error
A. Marks: 95 B. TypeError C. Marks: 95.0 D. Python automatically converts 95 to a string
A. :2d B. :.2f C. :%2 D. :round
A. print(item, end=" ") B. print(item, line=False) C. print(item, newline=0) D. print(item, stay=True)
Create a file named:
Then write:
This program combines:
print()sependTry changing:
to your own name.
Then change the marks:
Run the program again and observe how the output changes.
Try adding:
Then print:
using an f-string.
In the next lesson, we will continue your Python learning journey with Multiple Assignments (2: Variables).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Creating a Variable | Multiple Assignments |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.