How to Print in Python

Printing in Python is a fundamental concept that allows you to display text or variables on the screen. The primary function used for printing in Python is print().

To print a simple text message, you can use the following syntax:

print('Hello, World!')

This will output Hello, World! to the screen.

You can also print the values of variables by separating them with commas:

name = 'Alice'
age = 30
print('Name:', name, 'Age:', age)

Multiple values will be automatically separated by a space.

Formatting can be applied to printed output using f-strings. For example:

name = 'Bob'
age = 25
print(f'My name is {name} and I am {age} years old.')

Additionally, you can use the end and sep parameters to customize the behavior of the print function. The end parameter specifies the character to print at the end, while sep defines the separator between the values. For instance:

print('Value 1', 'Value 2', 'Value 3', sep=' | ', end='\n')

This will print Value 1 | Value 2 | Value 3 on the same line.

By mastering the print function in Python, you can efficiently showcase output and debug your code.

Leave a Reply

Your email address will not be published. Required fields are marked *