In this source code example, we will demonstrate how to read input from a console or user in Python.
The input function reads a line from the input, converts it to a string (stripping a trailing newline), and returns that. The function takes an optional argument, which is written to standard output without a trailing newline if present.
The input function reads a line from the input, converts it to a string (stripping a trailing newline), and returns that. The function takes an optional argument, which is written to standard output without a trailing newline if present.
Python reading input example
The following Python example prints a prompt and reads a first name, last name, and age from the console. Then it prints all information to the console.Create a read_input.py file and add the following content to it:
#!/usr/bin/env python
firstName = input("Enter your first name:")
lastName = input("Enter your last name:")
age = input("Enter your age:")
print("First name -> ", firstName)
print("Last name -> ", lastName)
print("Age -> ", age)
Output:
$ ./read_input.py
Enter your first name:John
Enter your last name:Cena
Enter your age:35
First name -> John
Last name -> Cena
Age -> 35
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course