Input and Output Functions in Python

Overview of Input and Output Functions

In programming, every program or software requires an interface to interact with users. An interface serves as a medium that facilitates interaction between software and the user, enabling the user to provide input to the software while allowing the software to display output through the interface. To accomplish this in Python, interface commands are utilized, specifically input functions and output functions. The input() function is designated to receive input from the user, whereas the print() function is employed to display output directly on the screen.

The print() Function and Its Variations

The print() function is the primary mechanism in Python for displaying results on the screen. There are several structural variations of the print() function depending on the specific output requirements.

Printing a Simple Message

To display a straightforward literal text message, the text string is enclosed within quotation marks inside the print() function.

Syntax: print("Message to be displayed")

Example: print("Welcome to Python Programming")

Output: Welcome to Python Programming

Printing a Variable

To output the value stored within a variable, the variable name is placed directly inside the parentheses of the print() function without quotation marks.

Syntax: print(variable)

Example: x = 2print(x)

Output: 2

Printing Text with Variables

To combine descriptive label text with a variable value, a string literal and a variable are passed as arguments to the print() function, separated by a comma.

Syntax: print("String to be displayed", variable)

Example: x = 5y = 8z = x + yprint("The sum =", z)

Output: The sum = 13

Combining Strings and Variables

For more complex output formats requiring multiple distinct text strings and multiple variables, additional arguments are separated by commas within the print() call.

Syntax: print("string1", variable, "string2", variable, ...)