{"id":7788,"date":"2023-11-02T18:57:27","date_gmt":"2023-11-02T13:27:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=7788"},"modified":"2023-11-10T12:13:09","modified_gmt":"2023-11-10T06:43:09","slug":"python-user-input-from-keyboard-input-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-user-input-from-keyboard-input-function\/","title":{"rendered":"Python User Input from Keyboard \u2013 input() function"},"content":{"rendered":"
Explore more instances related to python concepts from Python Programming Examples<\/a> Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n The input() function has the following syntax:<\/p>\n On the console, the prompt string is printed, and the user is given control to enter the value. Print some helpful information to assist the user in entering the expected value.<\/p>\n Here’s an example of getting user input and printing it to the console.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The value given by the user is always transformed to a string before being allocated to a variable. To be sure, let’s use the type() method to determine the type of the input variable.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n There is no method to acquire a user input of an integer or any other kind. However, we can convert the entered string to an integer using the built-in functions.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n There is no method to acquire a user input of an float or any other kind. However, we can convert the entered string to an float using the built-in functions.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n In Python, using the input() function to get user input is fairly simple. It’s primarily used to give the user an option of operation and then change the program’s flow accordingly. User Input from Keyboard \u2013 input() function in Python input() function Syntax of input() function Getting User Input using input() function Getting the type of entered input value Getting integer as User input Getting float as User Input Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner …<\/p>\n1)input() function<\/h3>\n
\n
2)Syntax of input() function<\/h3>\n
input(prompt)<\/code><\/p>\n
3)Getting User Input using input() function<\/h3>\n
value = input(\"Enter the value \")\r\nprint(\"the value of entered input = \", value)\r\n<\/pre>\n
Enter the value BTechGeeks\r\nthe value of entered input = BTechGeeks<\/pre>\n
4)Getting the type of entered input value<\/h3>\n
value = input(\"Enter the value which is of type string \")\r\nprint(\"the type of entered input value = \", type(value))\r\nvalue1 = input(\"Enter the value which is of type integer \")\r\nprint(\"the type of entered input value = \", type(value1))\r\n<\/pre>\n
Enter the value which is of type string BTechGeeks\r\nthe type of entered input value = <class 'str'>\r\nEnter the value which is of type integer 356\r\nthe type of entered input value = <class 'str'><\/pre>\n
5)Getting integer as User input<\/h3>\n
value = input(\"Enter the value \")\r\nintValue = int(value)\r\nprint(\"the type of entered input value = \", type(intValue))\r\n<\/pre>\n
Enter the value 4312\r\nthe type of entered input value = <class 'int'><\/pre>\n
6)Getting float as User Input<\/h3>\n
value = input(\"Enter the value \")\r\nfloatValue = float(value)\r\nprint(\"the type of entered input value = \", type(floatValue))\r\n<\/pre>\n
Enter the value 4561823.198\r\nthe type of entered input value = <class 'float'><\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n