If you want to take inputs from the console in your Python program then you can make use of the built-in function called input()
Let's see an example where the user is prompted to enter his name and a greeting message is displayed back.
# Code2care PROGRAM Programming:
# This program demonstrates
# how you can collect input
# from the console using the
# built in input() function
# in python.
print("Hello there! Please Enter your name:")
name = input()
print('Hello! {0}, How are you doing today?'.format(name))
Program Output:
Hello there! Please Enter your name: Chris
Hello! Chris, How are you doing today?
Process finished with exit code 0
As you can see after the first print message the console waits for the user to input something in the console, once the user press enters the program move ahead and the inputted string is stored in the variable.
What if the program needs two inputs from the user?
This is easy, all you will do is make use of the input() function twice in your code wherever it is required, let's see an example where you want to add two numbers inputted by the user,
# Code2care PROGRAM Programming:
# Add two numbers using two numbers inputted by
# user in the console
print('This Program Adds Two Numbers inputted by the user in console:')
print('Enter 1st Number:')
number1 = int(input())
print('Enter 2nd Number:')
number2 = int(input())
sumOfNumbers = number1 + number2
print('Sum: {0} + {1} = {2}'.format(number1, number2, sumOfNumbers))
Note that we have typecasted the input string as an int in order to treat it as an integer.
Have Questions? Post them here!
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
- How to check your installed version of Git - Git
- Notepad++ select all above or below lines - NotepadPlusPlus
- [Mac] How to Mute People in a Microsoft Teams Meeting - Teams
- Android Studio Button onClickListener Example - Android-Studio
- How to come out of Full Screen Mode Roblox on Mac - MacOS
- How to change background color in Notepad++ - NotepadPlusPlus
- Mac - Open Finder App using Terminal Current Location - MacOS
- How to change SharePoint Online site collection or subsite URL address - SharePoint