What is the basic usage of the print() function?

QuestionsQuestions8 SkillsProYour First Python LabAug, 15 2025
0190

The print() function in Python is used to output data to the console. Here are the basic usages:

Syntax

print(*objects, sep=' ', end='\n', file=None, flush=False)

Parameters

  • *objects: The values you want to print. You can pass multiple values separated by commas.
  • sep: A string that separates the objects. Default is a space.
  • end: A string that is printed at the end. Default is a newline character (\n).
  • file: An object with a write(string) method; defaults to the current sys.stdout.
  • flush: A boolean, if True, the output is flushed (forced to be written immediately).

Examples

  1. Basic Printing:

    print("Hello, World!")
  2. Printing Multiple Values:

    print("The answer is", 42)
  3. Custom Separator:

    print("Hello", "World", sep=", ")
  4. Custom End Character:

    print("Hello", end="!")
    print("World")
  5. Printing to a File:

    with open('output.txt', 'w') as f:
        print("Hello, File!", file=f)

These examples cover the basic usage of the print() function in Python.

0 Comments

no data
Be the first to share your comment!