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 a format method that can be used in conjunction with a new Formatter class to implement complex string formatting. More about these features can be found in the Python library reference manual.

Python - 3. Input and Output的更多相关文章

  1. [Python] Print input and output in table

    Print the input and output in a table using prettyTable. from prettytable import PrettyTable import ...

  2. [译]The Python Tutorial#7. Input and Output

    [译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...

  3. Python Tutorial 学习(七)--Input and Output

    7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...

  4. 7. Input and Output

    7. Input and Output There are several ways to present the output of a program; data can be printed i ...

  5. [20160704]Addition program that use JOptionPane for input and output

    //Addition program that use JOptionPane for input and output. import javax.swing.JOptionPane; public ...

  6. Input and Output File

    Notes from C++ Primer File State Condition state is used to manage stream state, which indicates if ...

  7. [20171128]rman Input or output Memory Buffers.txt

    [20171128]rman Input or output Memory Buffers.txt --//做一个简单测试rman 的Input or output Memory Buffers. 1 ...

  8. Angular4学习笔记(六)- Input和Output

    概述 Angular中的输入输出是通过注解@Input和@Output来标识,它位于组件控制器的属性上方. 输入输出针对的对象是父子组件. 演示 Input 新建项目connInComponents: ...

  9. Java中的IO流,Input和Output的用法,字节流和字符流的区别

    Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...

随机推荐

  1. Java如何对List集合的操作方法(一)

    目录: list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断: 利用list中索引 ...

  2. 如何在win+r 或者是win10的应用搜索输入subl就能打开sublime

    这虽然不是什么技术贴,我实在不想开启sublime还要动鼠标,或者输入subl长长的全称,这里有两种做法: 第一种 在环境变量添加sublime安装目录的变量,一般sublime的安装目录会有subl ...

  3. gitlab访问用户安装的postgresql数据库

    1.先将gitlab默认安装的postgresql的数据库中的数据,导入到用户安装的postgresql数据 用Navicat迁移数据.函数不用迁移. 2.配置gitlab对postgresql数据库 ...

  4. 解决因为Telnet没有启动导致FTP无法连接的问题

    今天ytkah在其他电脑上想用ftp传点东西发现居然连接不上,查看了一下服务器安全组规则里的端口,也没有相关屏蔽.问了一下运维,他说可能是Telnet没有开启.就试着去看看有没问题.打开 控制面板 - ...

  5. shell- 字符串处理 、 扩展的脚本技巧 、 正则表达式

    字符串截取的方法 方法一:使用${}表达式 格式:${var:起始位置:长度} 方法二:使用expr substr 格式:expr substr "$var" 起始位置 长度 方法 ...

  6. SQL SERVER 2016研究五

    SQL SERVER 2016 Row Level Security 以前:SQL server 的安全模型只能针对于它的表和列, 如果要针对于行,就需要创建存储过程或者函数来处理. 如何设置这个行级 ...

  7. 【剑指offer】最小的K个数

    一.题目: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.思路: 一群大牛在讨论用噼里啪啦各种排序,复杂度一般也都是O ...

  8. 搭建简单SBT工程实践

    在本机jdk(主要配置环境变量).scala(主要配置环境变量).sbt(主要配置①私服repositories  ②sbtconfig.txt)都已经安装且配置好的情况下. repositories ...

  9. Swagger Editor Linux安装(全新环境)

    查看内核版本 cat /proc/version cat /etc/redhat-release 查看系统是32位还是64位方法总结getconf LONG_BIT 安装相关工具 yum instal ...

  10. word2vec 评测 window_different

    This is a test for word2vecWed Nov 07 16:04:39 2018dir of model1: ./model/window3_ min_count2_worker ...