In Python, the input() function is used so that the user can input data. Whatever you enter, this function will convert it to a string. Even if you enter a number, the function will convert it to a string.
The following article is a detailed guide on how to use the input() function in Python as we go through the section below.
print ("What is your name?")
x = input()
print("You are " + x + ", aren't you?") For example, I will enter the name Python. Output:
What is your name?
You are Python, aren’t you?
input() function allows you to input data.
input(data)
Parameter Values:
data: a string. For example: name, number, …
Example 1: Basic input() function
print ("Enter your mother name:")
x = input()
print("What a beautiful name!") Output:
Enter your mother name:
What a beautiful name!
Example 2: Plus 1. Because the function will convert the input to a string. So if you want to input as int then use int()
print("Enter a number")
x = int(input())
plus = x + 1
print(plus) Output:
Enter a number: 11
12
Example 3: Parity check program
def check(x):
return (bool(x % 2 == 0))
print("Enter a number")
x = int(input())
if (check(x)):
print("It is an even number")
else:
print("It is an odd number") Output:
Enter a number: 8
It is an even number
Hope you understand our tutorial on how to use the input() function in Python.
Thanks for reading!
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.
Open source has come a long way. Recently I was watching a keynote address by…
Sylva 1.5 becomes the first release to include Kubernetes 1.32, bringing the latest open source…
Expansion ensures business continuity without forcing major upgrades Today, Canonical announced the expansion of the…
TL;DR: YARD-Lint catches documentation issues, just like RuboCop for code. Star it and use it…
Deploy a FedRAMP-ready kubernetes cluster and application suite, with FIPS 140-3 crypto and DISA-STIG hardening…
This new release brings the stability and security of Ubuntu to Axion-based N4A virtual machines…