Python Basic Input and Output
1. Using print() for Standard Output
print("Hello, Python!")
print(100)
print(3.14)The print() function displays text and values to the standard output (console).
2. Printing Multiple Values
name = "Alice"
age = 30
print(name, age)
# Output: Alice 30Multiple values can be printed in a single call, separated by spaces by default.
3. Customizing Output with sep and end
print("Python", "is", "powerful", sep=" - ", end="!")
# Output: Python - is - powerful!sepdefines the separator between values.enddefines what is printed at the end (default is newline).
4. Formatted Output using f-strings
f-strings provide a readable and efficient way to format strings.
5. Using str.format() Method
An alternative formatting approach, useful for older Python versions.
6. String Formatting with Old-Style % Operator
Legacy formatting style, still seen in legacy codebases.
7. Using input() to Receive User Input
input() always returns data as a string.
8. Converting Input to Required Data Type
Explicit conversion is required when numeric operations are needed.
9. Reading Multiple Inputs in One Line
Useful for command-line programs requiring space-separated values.
10. Redirecting Output to a File
The print() function can send output to files using the file parameter.
Last updated