Python String Input Output
Last Updated :
12 Apr, 2025
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.
Python
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.
Python
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.
Python
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.
Python
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.
Python
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.
Python
print("Python", "Java", "C++", sep=" | ")
OutputPython | 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.
Python
print("Loading", end="... ")
print("Please wait")
OutputLoading... 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.
Python
with open("output.txt", "w") as f:
print("This text will go into a file.", file=f)
Output
Output.txt file
Similar Reads
Get User Input in Loop using Python In Python, for and while loops are used to iterate over a sequence of elements or to execute a block of code repeatedly. When it comes to user input, these loops can be used to prompt the user for input and process the input based on certain conditions. In this article, we will explore how to use fo
3 min read
Input Validation in Python String In Python, string input validation helps ensure that the data provided by the user or an external source is clean, secure and matches the required format. In this article, we'll explore how to perform input validation in Python and best practices for ensuring that strings are correctly validated.Pyt
2 min read
Write Os.System Output In File Using Python Python is a high-level programming language. There are many modules. However, we will use os.system module in this Program. This module provides a portable way of using operating system-dependent functionality. The "os" and "os.path()" modules include many functions to interact with the file system.
3 min read
Output of Python Programs | Set 19 (Strings) 1) What is the output of the following program? PYTHON3 str1 = '{2}, {1} and {0}'.format('a', 'b', 'c') str2 = '{0}{1}{0}'.format('abra', 'cad') print(str1, str2) a) c, b and a abracad0 b) a, b and c abracadabra c) a, b and c abracadcad d) c, b and a abracadabra Ans. (d) Explanation: String function
3 min read
Python - Convert None to empty string In Python, it's common to encounter None values in variables or expressions. In this article, we will explore various methods to convert None into an empty string.Using Ternary Conditional OperatorThe ternary conditional operator in Python provides a concise way to perform conditional operations wit
2 min read