Python - 3. Input and Output
from:http://interactivepython.org/courselib/static/pythonds/Introduction/InputandOutput.html
Input and Output
- Input
Python’s input function takes a single parameter that is a string. This string is often called the prompt because it contains some helpful text prompting the user to enter something. For example, you might call input as follows:
aName = input('Please enter your name: ')
aName = input("Please enter your name ")
print("Your name in all capitals is",aName.upper(),
"and has length", len(aName))
It is important to note that the value returned from the input function will be a string representing the exact characters that were entered after the prompt. If you want this string interpreted as another type, you must provide the type conversion explicitly.
sradius = input("Please enter the radius of the circle ")
radius = float(sradius)
diameter = 2 * radius
Output (String Formatting)
>>> print("Hello")
Hello
>>> print("Hello","World")
Hello World
>>> print("Hello","World", sep="***")
Hello***World
>>> print("Hello","World", end="***")
Hello World***
>>>print(aName, "is", age, "years old.")
>>>print("%s is %d years old." % (aName, age))
The % operator is a string operator called the format operator.
| Character | Output Format |
|---|---|
d, i |
Integer |
u |
Unsigned integer |
f |
Floating point as m.ddddd |
e |
Floating point as m.ddddde+/-xx |
E |
Floating point as m.dddddE+/-xx |
g |
Use %e for exponents less than −4 −4 or greater than +5 +5 , otherwise use %f |
c |
Single character |
s |
String, or any Python data object that can be converted to a string by using the str function. |
% |
Insert a literal % character |
In addition to the format character, you can also include a format modifier between the % and the format character. Format modifiers may be used to left-justify or right-justifiy the value with a specified field width.
| Modifier | Example | Description |
|---|---|---|
| number | %20d |
Put the value in a field width of 20 |
- |
%-20d |
Put the value in a field 20 characters wide, left-justified |
+ |
%+20d |
Put the value in a field 20 characters wide, right-justified |
0 |
%020d |
Put the value in a field 20 characters wide, fill in with leading zeros. |
. |
%20.2f |
Put the value in a field 20 characters wide with 2 characters to the right of the decimal point. |
(name) |
%(name)d |
Get the value from the supplied dictionary using name as the key. |
>>> price = 24
>>> item = "banana"
>>> print("The %s costs %d cents"%(item,price))
The banana costs 24 cents
>>> print("The %+10s costs %5.2f cents"%(item,price))
The banana costs 24.00 cents
>>> print("The %+10s costs %10.2f cents"%(item,price))
The banana costs 24.00 cents
>>> itemdict = {"item":"banana","cost":24}
>>> print("The %(item)s costs %(cost)7.1f cents"%itemdict)
The banana costs 24.0 cents
>>>
In addition to format strings that use format characters and format modifiers, Python strings also include aformatmethod that can be used in conjunction with a newFormatterclass to implement complex string formatting. More about these features can be found in the Python library reference manual.
Python - 3. Input and Output的更多相关文章
- [Python] Print input and output in table
Print the input and output in a table using prettyTable. from prettytable import PrettyTable import ...
- [译]The Python Tutorial#7. Input and Output
[译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...
- Python Tutorial 学习(七)--Input and Output
7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...
- 7. Input and Output
7. Input and Output There are several ways to present the output of a program; data can be printed i ...
- [20160704]Addition program that use JOptionPane for input and output
//Addition program that use JOptionPane for input and output. import javax.swing.JOptionPane; public ...
- Input and Output File
Notes from C++ Primer File State Condition state is used to manage stream state, which indicates if ...
- [20171128]rman Input or output Memory Buffers.txt
[20171128]rman Input or output Memory Buffers.txt --//做一个简单测试rman 的Input or output Memory Buffers. 1 ...
- Angular4学习笔记(六)- Input和Output
概述 Angular中的输入输出是通过注解@Input和@Output来标识,它位于组件控制器的属性上方. 输入输出针对的对象是父子组件. 演示 Input 新建项目connInComponents: ...
- Java中的IO流,Input和Output的用法,字节流和字符流的区别
Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...
随机推荐
- RENAME方法进行分区改造
1.新建零时表 set time on timing on create table RPTUSER.RPT_TMP_10086BDL_bak ( HANDLEDATE DATE, TEL_CALLI ...
- vue关于html页面id设置问题
vue是一个前端框架,类似于angularJS等,vue在编写的时候需要在html页面指定id,但是不是都可以实现的,一般放在id需在div设置里才可以实现. (一) 在html里设置id: < ...
- sql-server数据库常用语句
查看所有数据库名 select name from master..Sysdatabases order by name; 查看当前数据所有表 select name from sysobjects ...
- NOIP2009靶形数独
题目描述: 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“ ...
- 关系型数据库与NoSQL数据库的优劣
当大家学习了一定的NoSQL知识以后,了解了现今许多NoSQL数据库(如HBase,MongoDB,Redis等)时,就会觉得关系型数据库可能已经跟不上时代的步伐.其实并不然,关系型数据库的性能绝对不 ...
- MongoDB 查询$关键字 $in $or $all
属于:$in 满足其中一个元素的数据把age=13,73 的数据显示 > db.user.find({age: { $in:[13,73]}}) { "_id" : Obje ...
- dedecms自定义表单提交成功后提示信息修改和跳转链接修改
我们在用dedecms自定义表单提交成功后提示信息一般是"Dedecms 提示信息",这个要怎么改成自己想要的文字呢?还有就是提示页停留时间,目前估计就2秒,太快了,要如何设置长点 ...
- zookeeper三种模式安装详解(centos 7+zookeeper-3.4.9)
zookeeper有单机.伪集群.集群三种部署方式,可根据自己实际情况选择合适的部署方式.下边对这三种部署方式逐一进行讲解. 一 单机模式 1.下载 进入要下载的版本的目录,选择.tar.gz文件下载 ...
- 封装||property
封装 封装:主要是指在类的定义阶段将,以__开头的属性名进行变形..例如:__name ==> _People__name 封装的主要特点: 1.在类外部无法直接__name,想要在外部调用可以 ...
- SQL IN查询优化
实际项目中有如下SQL, 发现效率很低,用时超过1分钟 select TaskID, StartDate = min(UpdateTime), EndDate = max(UpdateTime) fr ...