Example 1: Without Prompt
Name = input() # The data you enter will be assigned to a Name variable.
Example 2: With Prompt
Name = input(“What is your name?”) #Here, the prompt is “What is your Name?”.
Also see, Swapcase in Python
Example 3: Typecast string input to another data type
Python stores all variables as strings; to explicitly typecast them, we can use int(), float(), and many other functions along with the input() method. Let’s see how this works.
num = int(input(“Enter a number: “)) #Changes string input to int
So far, we have discussed how to input from the user in Python. Now, let’s proceed to see how to print an output onto the screen?
Output using the print() function
The print() function in python is used to display the output on the screen/console.
Simply call the print function and pass the parameters. Note: Parameters are the values you write within the opening and closing bracket.
For example: find(a, b), here find is a function with ‘a’ and ‘b’ as parameters. The actual syntax of the print() function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
- objects: The value(s) to be printed are called objects.
- sep: separator is used between the values. It defaults into a space character.
- end: It is used after all values are printed. A new line is created by default.
-
flush: whether to forcibly flush the stream.
Remember, the values of sep and end can be modified. For example, it can be modified to a comma, hash, special symbol, number, etc.
Below are some examples of how print() can be used.
Example 1: Without Parameters
print(“Welcome to Coding Ninjas”)
Output:
Welcome to Coding Ninjas
Example 2: With Parameters
num1=5
num2=10
sum=num1+num2
print("Addition of",num1,"and",num2,"equals to",sum)
The output of the above code after passing num1,num2 and sum as parameters will be:
Addition of 5 and 10 equals to 15
Using print() in more detail
-
Let’s say you wish to print a particular string (a collection of characters such as letters, punctuation marks, digits, and letters) N times. On strings, the (asterisk) * operator performs repetition. Put “A” followed by * and the number of times you want “A” to be repeated inside the print parenthesis.
print(“A”*6)
Output:
AAAAAA
- We may want to format the output to make it more appealing. The str.format() method can be used to accomplish this.
x = 5
y = 10
string = 'The value of x is {} and y is {}'
print(string.format(x, y)) #or you could simply write
print('The value of x is {} and y is {}'.format(x, y))
Output:
The value of x is 5 and y is 10
The value of x is 5 and y is 10
-
Curly braces {} are utilised as placeholders in str.format(). We can use numbers(tuple index) to specify the order in which they are printed.
print('I am good in {0} and {1}'.format('Maths','Coding'))
print('I am good in {1} and {0}'.format('Maths','Coding'))
Output:
I am good in Maths and Coding
I am good in Coding and Maths
Moving on from Python inputs and outputs, how do we handle the data that the user enters as input?
We require a particular level of functionality from the code. We have two options for this as well:
- Create the functionality from scratch.
-
Use existing libraries to continue our program.
It can be simple to create functions from scratch, but it can also be time-consuming and frustrating. Because Python is so widely used, the simplest and most favored method is to use libraries. Hundreds of thousands of libraries are available to use in Python.
So, let’s work with user input and add certain functionalities.
You can also read about the Multilevel Inheritance in Python, Fibonacci Series in Python
Check out Escape Sequence in Python
Python Import
It is a good idea to separate our code into different modules as it gets bigger.
And In Python, It can be done using Modules.
- A module is a Python file that contains definitions and statements.
- Modules in Python have a filename and a.py extension.
-
A module’s definitions can be imported into another module or Python’s interactive interpreter. To do so, we use the import keyword.
Assume the user enters a number, and we want our code to return the number’s square root.
The function sqrt() is found in the math module. To utilise this function, we must first import the module. So, let’s have a look at how importing the module will give our code a boost.
#first import math module
import math
#get a variable having value 25
number=25
#square root this number.
number=math.sqrt(number)
print(number)
You will get the square root of 25 (which is 5) if you run this code.
Output:
5
We can also access specific attributes from the module using the from keyword. “Import this function, method, or attribute from this module,” it says.
Consider the following case:
from math import pi
print(pi)
Output:
3.141592653589793
Let’s move on to some frequently asked questions on Python Input Output (I/O) & Import functions.
Check out this article - String slicing in Python.
Must Read Invalid Syntax in Python.
Frequently Asked Questions
How do you input and output in Python?
In Python, the input and output are done using built-in functions input() and print().
How do you get output in Python?
In Python, print() is used to display output on the screen/console.
What is input() in Python?
In Python, input() is a built-in function that helps in taking input from the user.
What is the default data type of input statement in Python 3?
A string is the default data type of input statement in Python 3.
What does raw input mean in Python?
The raw input() method in Python is used to read a string from a standard input device such as a keyboard.
How do you import something into Python?
We can import modules using the import keyword.
Is a Python file a module?
Any Python file can be referenced as a module. A file containing Python code, for example, test.py, is called a module.
Conclusion
Superb! Indeed you learned everything you need to know about accepting user input in Python and printing the needed outputs. We also learned how to use Python’s import command to extend the functionality of our code and interact with inputs.
Recommended Readings: