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 aformat
method that can be used in conjunction with a newFormatter
class 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).把 硬盘上的数据读取到内存中,这种操作 成为 ...
随机推荐
- Java问题汇总
一.java编译通过,为什么运行却提示找不到或无法加载主类 使用运行窗口编译通过,但运行提示 通过eclipse可以运行 原因:代码中有package javaLearn:编译时在javaLearn目 ...
- Pytorch安装教程
一.准备 Window10系统+Ubuntu16.10系统.Anaconda3.5(python3.6) 二.流程 (1)由于墙的问题,用conda安装Pytorch过程中会连接失败,这是因为Anac ...
- pacakge-info.java
翻看以前的笔记,看到一个特殊的java文件:pacakge-info.java,虽然有记录,但是不全,就尝试着追踪一下该问题, 分享一下流水账式的结果. 首先,它不能随便被创建.在Eclipse中, ...
- Python接口自动化【requests处理Token请求】
首先说一下使用python模拟登录或注册时,对于带token的页面怎么登录注册模拟的思路: 1.对于带token的页面,需要先从最开始的页面获取合法token 2.然后使用获取到的合法token进行后 ...
- java执行字符串数学表达式【记录】
https://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form 1. goo ...
- SQL Server 2016 发送邮件功能
--1 安装好SQL Server 2016 --2 安装.Net 3.5 由于SQL Server 2016 安装不提示强制安装.NET 3.5 但是还是需要安装,数据库发送邮件会使用.NET 3. ...
- 基于TensorFlow的简单验证码识别
TensorFlow 可以用来实现验证码识别的过程,这里识别的验证码是图形验证码,首先用标注好的数据来训练一个模型,然后再用模型来实现这个验证码的识别. 生成验证码 首先生成验证码,这里使用 Pyth ...
- 网站分析参数(实例分析)SimilarWeb插件参数
网站分析参数(实例分析)SimilarWeb插件参数 那么这些指标是什么意思呢? SimilarWeb Rank:类似网站排名Global Rank:全球网站排名第三栏一般是类别,大概网站从事范围 ...
- 用PE系统安装原版XP
方法:直接运行Winnt32程序进行XP原版系统安装. [1].在PE系统中将XP SP3系统镜像ISO文件从U盘上复制到硬盘的非系统分区后,用PE所带WinRAR程序将该ISO镜像中的I386 ...
- Scala枚举--Enumeration
object Color extends Enumeration(2){ val Red,Green,Blue = Value val Yellow = Value("YELLOW" ...