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).把 硬盘上的数据读取到内存中,这种操作 成为 ...
随机推荐
- 【PyQt5-Qt Designer】QComboBox-下拉列表框
知识点: 1.QComboBox下拉列表框的一些常用方法 2.下拉列表框常用信号使用方法 案例:选中下拉框选项时触发信号 #[str] 表示comboBox中的选择框内容如A B C D 等 self ...
- Javabean非空变量校验工具
摘要 调研java Class getDeclaredFields() 与getFields()的区别,设计请求参数为Javabean时,基于反射机制校验其中非空变量的公共方法. java Class ...
- Eclipse设置注释模板和工作空间背景色为豆沙绿
Eclipse Version: Photon Release (4.8.0). 首先上图,根据图上的步骤即可完成注释模板的设置. 1 如何设置eclipse注释模板的日期格式 在eclipse的 P ...
- 测试:fiddler使用
Fiddler是个很强大的工具,很多新人可能不知道怎么用. 直接下载安装,然后打开电脑端的Fiddler,点击Tools > Fiddler Options,勾选上 Allow remote c ...
- 如何暂停和继续运行Linux程序
我们通过shell窗口运行程序时,由于有的程序长时间运行,直到下班了都还没有返回运行结果.这个时候,我们又不能直接关闭shell窗口,不然前面的时间就白白运行了. 那有什么办法可以先暂停程序,明天再继 ...
- 用A标签实现页面内容定位 点击链接跳到具体位置
经常在维基百科等网站看到目录列表,点击链接会跳到具体的位置,小美眉一直在问是怎么做到的,其实挺简单的,用A标签实现页面内容定位就行了.实例参考微信营销理论手册的目录. 首先用A标签定义目录的链接. & ...
- WIN32,_WIN32_WIN64
MSDN 里说,VC 有 3 个预处理常量,分别是 _WIN32,_WIN64,WIN32. 只要包含了 Windows.h,那么 WIN32 常量是肯定定义了的,所以不能用于判断平台环境(如果x64 ...
- Linux个人知识扩展:服务器几u的意思,网络带宽
服务器几u的意思: 指的服务器大小规格 1U=4.45cm 2U=8.9cm 3U=4.45cm * 3 4U=4.45cm * 4 这指的是服务器的高度 现在的服务器为节省空间都是很扁的 U是服务器 ...
- jenkins 的一个BUG
最近更新了一批jenkin插件,更新完问题来了全局设置无法保存了... 报错如下 Stack trace net.sf.json.JSONException: null object at net.s ...
- [LeetCode] 876. Middle of the Linked List_Easy tag: Linked List ** slow, fast pointers
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...