×


Python input() Function in Linux



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.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Python function queries.

In this context, we shall look into how to use the input() function in Python.


More about Python input() Function ?

input() function allows you to input data.

Its syntax is given below:

input(data)

Parameter Values is:

data: a string. For example: name, number, …


Examples of using Python input() Function

1. Consider the below function:

print ("What is your name?")
x = input()
print("You are " + x + ", aren't you?")
For example, We will enter the name Python. 

The Output would be:

What is your name?
You are Python, aren’t you?


2. Basic input() function:

print ("Enter your mother name:")
x = input()
print("What a beautiful name!")

Output would be:

Enter your mother name:
What a beautiful name!


3. 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)

The Output would be:

Enter a number: 11
12


4. 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")

The Output would be:

Enter a number: 8
It is an even number


[Need assistance in fixing Python function issues ? We can help you. ]


Conclusion

This article covers how to use the input() function in Python. In fact, The input() function in Python makes it very easy to get input from users of your application. The input function is built in just like the print function and its use is quite common. When you call the input function(), you can think of it as an expression that evaluates to whatever the user typed in at the prompt. This always gets returned as a string so if you want to use numbers as input, you will need to handle that by using the int() function.


More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com