8.1 User Input-Output (Modified)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Reading Keyboard Input

Python allows for user input. That means we are able to ask the user for input. Python version
3.6 uses the input() method. Python version 2.7 uses the raw_input() method. The following
example asks for the username, and when you entered the username, it gets printed on the
screen.
Python provides two built-in functions to read a line of text from standard input, which by
default comes from the keyboard. These functions are −
 raw_input
 input
The raw_input Function: The raw_input([prompt]) function reads one line from standard input
and returns it as a string (removing the trailing newline).

str = raw_input("Enter your input: ")


print "Received input is : ", str

This prompts you to enter any string and it would display same string on the screen. When I type
"Hello Python!", its output is like this −
Enter your input: Hello Python
Received input is : Hello Python
The input Function: The input([prompt]) function is equivalent to raw_input, except that it
assumes the input is a valid Python expression and returns the evaluated result to you.

str = input("Enter your input: ")


print "Received input is : ", str

This would produce the following result against the entered input −

Enter your input: [x*5 for x in range(2,10,2)]


Received input is : [10, 20, 30, 40]
Printing to the Screen
The simplest way to produce output is using the print statement where you can pass zero or more
expressions separated by commas. This function converts the expressions you pass into a string
and writes the result to standard output as follows −
print "Python is really a great language,", "isn't it?"
This produces the following result on your standard screen −
Python is really a great language, isn't it?
String format()
The format() method allows you to format selected parts of a string. Sometimes there are parts of
a text that you do not control, maybe they come from a database, or user input? To control such
values, add placeholders (curly brackets {}) in the text, and run the values through
the format() method:

Example: Add a placeholder where you want to display the price:


price = 49
txt = "The price is {} dollars"
print(txt.format(price))
Result: The price is 49 dollars

You can add parameters inside the curly brackets to specify how to convert the value:

Example: Format the price to be displayed as a number with two decimals:


txt = "The price is {:.2f} dollars"
Result: The price is 49.00 dollars

Multiple Values
If you want to use more values, just add more values to the format() method and add more
placeholders.
print(txt.format(price, itemno, count))

Example: quantity = 3
itemno = 567
price = 49
myorder = "I want {} pieces of item number {} for {:.2f} dollars."
print(myorder.format(quantity, itemno, price))
Result: I want 3 pieces of item number 567 for 49.00 dollars.

Index Numbers
You can use index numbers (a number inside the curly brackets {0}) to be sure the values are
placed in the correct placeholders.

Example: quantity = 3
itemno = 567
price = 49
myorder = "I want {0} pieces of item number {1} for {2:.2f} dollars."
print(myorder.format(quantity, itemno, price))

Also, if you want to refer to the same value more than once, use the index number.

Example: age = 36
name = "John"
txt = "His name is {1}. {1} is {0} years old."
print(txt.format(age, name))

Named Indexes
You can also use named indexes by entering a name inside the curly brackets {carname} but
then you must use names when you pass the parameter values txt.format(carname = "Ford").

Example: myorder = "I have a {carname}, it is a {model}."


print(myorder.format(carname = "Ford", model = "Mustang"))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy