Python offers a straightforward way to interact with the user through the print() and input() functions. The print() function displays information on the console, taking one or more arguments like strings, numbers, or variables. It can also be used to format output, write to files, and display results in real time. The input() function prompts the user for input, capturing it as a string. This input can be converted to numbers using int() or float() and used in calculations or further processing. By combining these functions, you can create dynamic and interactive Python programs that respond to user input and provide meaningful output.
Here is an overview of both functions.
print(object(s), sep=separator, end=end, file=file, flush=flush)
Parameter | Description |
---|---|
object(s) | These are the values you want to print. You can provide zero or more objects, which will be converted to strings before being printed. |
sep=’separator’ | This specifies how multiple objects should be separated when printed. By default, it’s a space (‘ ‘ ), but you can change it to any string. |
end=’end’ | This determines what is printed at the very end of the output. By default, it’s a newline character (‘\n’), which moves the cursor to the next line. You can customize this as well. |
file | This parameter defines where the output should be sent. By default, it’s sys.stdout (your console), but you can redirect the output to a file-like object for writing to a file. |
flush | This is a boolean value that controls whether the output is flushed (forced to be written immediately) or buffered (held in memory until a certain amount is accumulated). By default, it’s False (buffered), but you can set it to True to flush the output. Export to Sheets |
input(prompt)
Parameter | Description |
---|---|
prompt (optional) | A string that is displayed to the user as a prompt before they enter their input. If omitted, no prompt is displayed. |