Print Function

Nerd Cafe

What is print() in Python?

The print() function in Python is used to display output to the console.

print("Hello, World!")

Output:

Hello, World!

Syntax of print()

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Parameter
Description

*objects

One or more values to print

sep

Separator between values (default is space ' ')

end

What to print at the end (default is newline '\n')

file

Where to send the output (default is screen/console)

flush

Whether to forcibly flush the stream

Step-by-Step: Practical Examples

1. Basic String Output

print("Machine Learning is awesome!")

Output:

2. Printing Variables

Output:

Practical Tip for ML: Use this to report intermediate results like accuracy, loss, epoch number, etc.

3. Using sep for Formatting

Output:

Tip: Use sep when printing multiple ML metrics in one line.

4. Using end to Stay on Same Line

Output:

Tip: Useful for printing progress, epochs, or loops in ML.

5. Print Multiple Data Types

Output:

Tip: Always confirm data types when preprocessing data in ML.

6. f-Strings for Clean ML Logging (Python 3.6+)

Output:

Tip: f-strings are the most common and readable way to log training progress.

7. Redirecting Output to a File

Tip: Use this in ML to save logs, especially when training on remote servers.

8. Using flush=True for Real-Time Output

Tip: Helpful when printing training progress bars or live metrics in the console.

Summary

Concept
Example

Basic Print

print("Hello")

Variables

print("Accuracy:", acc)

sep

print("a", "b", sep="-")

end

print(i, end=" ")

f-String

print(f"Loss: {loss:.4f}")

To File

print("log", file=f)

Real-time

print(".", end="", flush=True)

Keywords

print function,python print,python output,python basics,print syntax,print examples,sep parameter,end parameter,flush output,print to file,f-strings,ml logging,debugging in ml,python for ml,python variables,real-time printing,training logs,python console output,print multiple values,print formatting, nerd cafe

Last updated