Open In App

Taking input from console in Python

Last Updated : 17 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

What is Console in Python? Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message. A Python Console looks like this. Here we write a command and to execute the command just press enter key and your command will be interpreted. For coding in Python, you must know the basics of the console used in Python. The primary prompt of the python console is the three greater than symbols

>>>

You are free to write the next command on the shell only when these prompts have appeared after executing the first command. The Python Console accepts commands in Python that you write after the prompt. Accepting Input from Console User enters the values in the Console and that value is then used in the program as it was required. To take input from the user we make use of a built-in function input()

Python




# input
input1 = input()
 
# output
print(input1)


We can also typecast this input to integer, float, or string by specifying the input() function inside the type.

1. Typecasting the input to Integer: There might be conditions when you might require integer input from the user/Console, the following code takes two input(integer/float) from the console and typecasts them to an integer then prints the sum. 

Python




# input
num1 = int(input())
num2 = int(input())
 
# printing the sum in integer
print(num1 + num2)


2. Typecasting the input to Float: To convert the input to float the following code will work out. 

Python




# input
num1 = float(input())
num2 = float(input())
 
# printing the sum in float
print(num1 + num2)


3. Typecasting the input to String: All kinds of input can be converted to string type whether they are float or integer. We make use of keyword str for typecasting. 

 we can also take input string by just writing input() function by default it makes the input string

Python




# input
string = str(input())
 
# output
print(string)
 
# Or by default
string_default = input()
 
# output
print(string_default)




Previous Article
Next Article

Similar Reads

Taking input in Python
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input ( prompt )raw_input ( prompt ) input (): This functi
3 min read
Ways to read input from console in Java
In Java, there are four different ways to read input from the user in the command line environment(console). 1. Using Buffered Reader ClassThis is the Java classical method to take input, Introduced in JDK1.0. This method is used by wrapping the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader, we can r
5 min read
Python VLC MediaPlayer – Taking Screenshot
In this article we will see how we can take screen shot of the MediaPlayer object in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. MediPlyer object is the basic object in vlc module for playing the video. A screenshot, als
2 min read
Taking Screenshots using pyscreenshot in Python
Python offers multiple libraries to ease our work. Here we will learn how to take a screenshot using Python. Python provides a module called pyscreenshot for this task. It is only a pure Python wrapper, a thin layer over existing backends. Performance and interactivity are not important for this library. Installation Install the package pyscreensho
2 min read
Taking multiple inputs from user in Python
The developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods.  Methods to Take multiple inputs from user Using split() method :Using map() with split():Using List comprehension : Using s
4 min read
Position after taking N steps to the right and left in an alternate manner
Given three integers N, A, and B. A person is standing at 0-th coordinate and moves A steps to the right in the first step, B steps to the left at the second step, and so on. The task is to find out at which coordinate he will be after N steps. Examples: Input: N = 3, A = 5 and B = 2 Output: 8 5 to the right, 2 to the left and 5 to the right, hence
3 min read
Min operations to reduce N by multiplying by any number or taking square root
Given a number N, the task is to find the minimum value of N by applying below operations any number of times: Multiply N by any positive integerReplace N with sqrt(N), only if N is a perfect square. Examples: Input: N = 20 Output: 10Explanation: Multiply -> 20 * 5 = 100 sqrt(100) = 10, which is the minimum value obtainable. Input: N = 5184 Outp
4 min read
Program to multiply two Matrix by taking data from user
Given two matrices, the task is to multiply them. The size and number of elements of matrices are to be read from the keyboard. Examples: Input: row1 = 2, col1 = 2, mat1[][] = {{1, 2}, {3, 4}}, row2 = 2, col2 = 2, mat2[][] = {{1, 1}, {1, 1}} Output: {{3, 3}, {7, 7}} Input: row1 = 2, col1 = 2, mat1[][] = {{2, 4}, {3, 4}} row1 = 2, col1 = 2, mat2[][]
15 min read
Python | Create simple animation for console-based application
As we know Python is a scripting language, and can be easily used to automate simple tasks. In this article, we will learn how to create a simple console-based animation, which can be used while developing a console based project as a utility. We will try to replicate the loading animation as shown below: We will be using following modules - sys mo
2 min read
Blackjack console game using Python
Blackjack is a popular two-player card game that is played with a deck of standard playing cards around the world in casinos. The main criteria for winning this game are chance and strategy. The challenge of this game is to get as close to 21 points as possible without exceeding them. That is why it is also known as Twenty-One. In this article, we
6 min read
Article Tags :
Practice Tags :