Rolex Pearlmaster Replica
  Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
This article is part of in the series
Published: Friday 1st March 2024

Python Input() Function: A Complete Guide

In Python, the input() function enables you to accept data from the user. The function is designed so that the input provided by the user is converted into a string.

In this brief guide, you'll learn how to use the input() function.

Syntax of the input() Function

The input() function is quite straightforward to use with this syntax:

input(prompt)

 

If you use the optional parameter, the function can accept a string written without a newline character in the standard output. It returns a string object.

Here's a simple example of the input() function in action:

IDnumber = input("Please enter your ID number: ")

print("Welcome, " + IDnumber + "!")

 

The output of this code is:

Please enter your ID number: 123

Welcome, 123!

 

Examples of input() Function

The input() function can be used in a variety of scenarios:

#1 Accepting an Input 

To take an input from the user and print it, you can write a simple Python script like this:

# Prompting user for input and storing it in a variable

userInput = input()

# Printing the user's input

print(userInput)

 

The output is:

Enter anything: print this

print this 

 

#2 Converting the Input to a Number

As mentioned earlier, the input() function accepts inputs and converts them into strings. However, you can change the data type of the accepted data before storing it.

Here's an example of accepting a number and adding 7 to it:

# Accepting an input and typecasting to an integer

number = int(input(“Type an integer: ”))

addition = number + 7

# Printing the output

print(addition)

 

The output of this code is:

Type an integer: 5

12

 

#3 Accepting a Float Input

Interestingly, you can typecast the data that input() accepts into floating-type data. The method of doing this is quite straightforward:

# Accepting an input and typecasting to a floating point number

number = float(input(“Type a number: ”))

addition = number + 7

# Printing the output

print(addition)

 

Running this code will generate this output:

Type a number: 8

15.0

 

#4 Accepting a List with input()

Converting the data that the input() function receives into a list is not as complicated as it may seem. Here's an example of accepting some data and storing it as a list:

# Accepting an input and converting it into a list

numberList = list(input(“Type some data: ”))

# Printing the output

print(numberList)

 

Here's what happens if you give this program some numbers:

Type some data: 74839

['7', '4', '8', '3', '9']

 

#5 Accepting Sets and Tuples

In the code below, we accept a string input from the user and convert it into a tuple before printing it:

# Accepting an input and converting it into a tuple

numberTuple = tuple(input(“Type some data: ”))

# Printing the output

print(numberTuple)

 

Here's what happens if you give this program some numbers:

Type some data: 74839

('7', '4', '8', '3', '9')

 

#6 Converting Input into a Dictionary

To turn an input into a dictionary, you must accept words separated by spaces. You can then make a dictionary of the words with their lengths as the values.

Let's see it in action:

stringVariable = input("Enter some words with spaces between them: ")

words = {word: len(word) for word in stringVariable.split()}

print(words)

 

The output is:

Enter some words with spaces between them: John Doe

{'John': 4, 'Doe': 3}

 

Latest Articles


Tags

  • Unpickling
  • array
  • sorting
  • reversal
  • Python salaries
  • list sort
  • Pip
  • .groupby()
  • pyenv global
  • NumPy arrays
  • Modulo
  • OpenCV
  • Torrent
  • data
  • int function
  • file conversion
  • calculus
  • python typing
  • encryption
  • strings
  • big o calculator
  • gamin
  • HTML
  • list
  • insertion sort
  • in place reversal
  • learn python
  • String
  • python packages
  • FastAPI
  • argparse
  • zeros() function
  • AWS Lambda
  • Scikit Learn
  • Free
  • classes
  • turtle
  • convert file
  • abs()
  • python do while
  • set operations
  • data visualization
  • efficient coding
  • data analysis
  • HTML Parser
  • circular queue
  • effiiciency
  • Learning
  • windows
  • reverse
  • Python IDE
  • python maps
  • dataframes
  • Num Py Zeros
  • Python Lists
  • Fprintf
  • Version
  • immutable
  • python turtle
  • pandoc
  • semantic kernel
  • do while
  • set
  • tabulate
  • optimize code
  • object oriented
  • HTML Extraction
  • head
  • selection sort
  • Programming
  • install python on windows
  • reverse string
  • python Code Editors
  • Pytest
  • pandas.reset_index
  • NumPy
  • Infinite Numbers in Python
  • Python Readlines()
  • Trial
  • youtube
  • interactive
  • deep
  • kernel
  • while loop
  • union
  • tutorials
  • audio
  • github
  • Parsing
  • tail
  • merge sort
  • Programming language
  • remove python
  • concatenate string
  • Code Editors
  • unittest
  • reset_index()
  • Train Test Split
  • Local Testing Server
  • Python Input
  • Studio
  • excel
  • sgd
  • deeplearning
  • pandas
  • class python
  • intersection
  • logic
  • pydub
  • git
  • Scrapping
  • priority queue
  • quick sort
  • web development
  • uninstall python
  • python string
  • code interface
  • PyUnit
  • round numbers
  • train_test_split()
  • Flask module
  • Software
  • FL
  • llm
  • data science
  • testing
  • pathlib
  • oop
  • gui
  • visualization
  • audio edit
  • requests
  • stack
  • min heap
  • Linked List
  • machine learning
  • scripts
  • compare string
  • time delay
  • PythonZip
  • pandas dataframes
  • arange() method
  • SQLAlchemy
  • Activator
  • Music
  • AI
  • ML
  • import
  • file
  • jinja
  • pysimplegui
  • notebook
  • decouple
  • queue
  • heapify
  • Singly Linked List
  • intro
  • python scripts
  • learning python
  • python bugs
  • ZipFunction
  • plus equals
  • np.linspace
  • SQLAlchemy advance
  • Download
  • No
  • nlp
  • machiine learning
  • dask
  • file management
  • jinja2
  • ui
  • tdqm
  • configuration
  • deque
  • heap
  • Data Structure
  • howto
  • dict
  • csv in python
  • logging in python
  • Python Counter
  • python subprocess
  • numpy module
  • Python code generators
  • KMS
  • Office
  • modules
  • web scraping
  • scalable
  • pipx
  • templates
  • python not
  • pytesseract
  • env
  • push
  • search
  • Node
  • python tutorial
  • dictionary
  • csv file python
  • python logging
  • Counter class
  • Python assert
  • linspace
  • numbers_list
  • Tool
  • Key
  • automation
  • website data
  • autoscale
  • packages
  • snusbase
  • boolean
  • ocr
  • pyside6
  • pop
  • binary search
  • Insert Node
  • Python tips
  • python dictionary
  • Python's Built-in CSV Library
  • logging APIs
  • Constructing Counters
  • Assertions
  • Matplotlib Plotting
  • any() Function
  • Activation
  • Patch
  • threading
  • scrapy
  • game analysis
  • dependencies
  • security
  • not operation
  • pdf
  • build gui
  • dequeue
  • linear search
  • Add Node
  • Python tools
  • function
  • python update
  • logging module
  • Concatenate Data Frames
  • python comments
  • matplotlib
  • Recursion Limit
  • License
  • Pirated
  • square root
  • website extract python
  • steamspy
  • processing
  • cybersecurity
  • variable
  • image processing
  • incrementing
  • Data structures
  • algorithm
  • Print Node
  • installation
  • python function
  • pandas installation
  • Zen of Python
  • concatenation
  • Echo Client
  • Pygame
  • NumPy Pad()
  • Unlock
  • Bypass
  • pytorch
  • zipp
  • steam
  • multiprocessing
  • type hinting
  • global
  • argh
  • c vs python
  • Python
  • stacks
  • Sort
  • algorithms
  • install python
  • Scopes
  • how to install pandas
  • Philosophy of Programming
  • concat() function
  • Socket State
  • % Operator
  • Python YAML
  • Crack
  • Reddit
  • lightning
  • zip files
  • python reduce
  • library
  • dynamic
  • local
  • command line
  • define function
  • Pickle
  • enqueue
  • ascending
  • remove a node
  • Django
  • function scope
  • Tuple in Python
  • pandas groupby
  • pyenv
  • socket programming
  • Python Modulo
  • Dictionary Update()
  • Hack
  • sdk
  • python automation
  • main
  • reduce
  • typing
  • ord
  • print
  • network
  • matplotlib inline
  • Pickling
  • datastructure
  • bubble sort
  • find a node
  • Flask
  • calling function
  • tuple
  • GroupBy method
  • Pythonbrew
  • Np.Arange()
  • Modulo Operator
  • Python Or Operator
  • Keygen
  • cloud
  • pyautogui
  • python main
  • reduce function
  • type hints
  • python ord
  • format
  • python socket
  • jupyter
  • Python is a beautiful language.