Python String Input Output
In Python, input and output operations are fundamental for interacting with users and displaying results. The input() function is used to gather input from the user and the print() function is used to display output.
Input operations in Python
Python’s input() function allows us to get data from the user. By default, all input received via this function is treated as a string. If we need a different data type, we can convert the input accordingly.
Example: input() can be used to ask users for a string, which is then returned as a string.
a = input("Enter your name: ")
print(a)
Output
Enter your name: vishakshi
vishakshi
Syntax of input()
input(prompt)
- Parameter: prompt (optional) is the string that is displayed to the user to provide instructions or information about what kind of input is expected.
- Returns: It returns the user input as string.
Examples of input()
Example 1: In this example, we take the length and width of a rectangle from the user using input(), convert them to float and then calculate the area.
l = float(input("Enter the length of the rectangle: "))
w = float(input("Enter the width of the rectangle: "))
a = l * w
print(a)
Output
Enter the length of the rectangle: 4
Enter the width of the rectangle: 8
32.0
Example 2: In this example, we take an integer from the user and print its square. The input is converted to int since we are dealing with whole numbers.
n = int(input("Enter a number: "))
res = n * n
print(res)
Output
Enter a number: 4
16
Example 3: In this example, the user enters a space-separated list of numbers in one line. We use input().split() along with map(int, ...) to convert the input into a list of integers.
a = list(map(int, input("Enter elements of the array: ").split()))
print(a)
Output
Enter elements of the array: 2 5 8 7 5 10
[2, 5, 8, 7, 5, 10]
Output operations in Python
In Python, the print() function is used to display output to the user. It can be used in various ways to print values, including strings, variables and expressions.
Example 1: The simplest form of output in Python is using the print() function.
print("Hello, World!")
Output
Hello, World!
Syntax of print()
print(object(s), sep=' ', end='\n', file=sys.stdout, flush=False)
Parameters:
Parameter | Description |
---|---|
object(s) | The value(s) to be printed. You can pass multiple values separated by commas. |
sep (Optional) | Separator between values. Default is a space ' '. |
end (Optional) | What to print at the end. Default is a newline '\n'. |
file (Optional) | Where to send the output. Default is sys.stdout (console). |
flush (Optional) | Whether to forcibly flush the output stream. Default is False. |
Returns: It returns None and is used only for displaying output.
Examples of print()
Example 1: In this example, multiple values are printed with " | " used as the separator instead of the default space. This is useful for formatting output with custom characters between values.
print("Python", "Java", "C++", sep=" | ")
Output
Python | Java | C++
Example 2: Here, the end parameter is set to "..." to avoid the default newline and print the next statement on the same line. This creates a smooth, continuous output message.
print("Loading", end="... ")
print("Please wait")
Output
Loading... Please wait
Example 3: In this example, the file parameter is used to redirect the output to a file named output.txt instead of printing it on the console. It’s commonly used for writing logs or saving results.
with open("output.txt", "w") as f:
print("This text will go into a file.", file=f)
Output
