python的基本输入输出
来源:互联网 发布:守望先锋 网络延迟严重 编辑:程序博客网 时间:2024/05/19 10:07
python的输入输出
输入:
input(), raw_input()
1. input(): 用于收集数字输入
>>>a=input("input a:")
input a:2
>>> a
2
#可以输入一个变量:
>> a=input("inputa:")
input a:a
>>> a
2
#无法接收字符
>>>a=input("input a:")
input a:b
Traceback (mostrecent call last):
File "<pyshell#7>", line 1,in <module>
a=input("input a:")
File "<string>", line 1, in<module>
NameError: name'b' is not defined
2. raw_input():把用户输入的任何内容都保存为一个字符串;
>>>a=raw_input('input a:')
input a:2
>>> a
'2'
3. 强制类型转换
>>>a='2'
>>>b=float(a)
>>> b
2.0
不能转换则会抛出错误
4. 密码输入
python 自带了一个库 getpass
>>>from getpass import getpass
>>>password=getpass()
password:
>>>password
'rrrr'
5. 清理用户的输入
使用strip()函数
6. 格式化输出
对字符串使用format()函数
例1
>>>greeting='Good {} ,{} are fine!'
>>>time='morning'
>>>people='I'
>>>print greeting.format(time,people)
Good morning ,I are fine!
花括号的值和format()中的变量一一对应
例2
greeting='Good{time}, {people} are fine'
>>>print greeting.format(time='morning',people='I')
Good morning,I are fine
>>>greeting
'Good{time}, {people} are fine'
>>>time='morning'
>>>people='I'
>>>print greeting.format(time=time,people=people)
Good morning, I are fine
例3
people='{0},{1},{2}'
>>> print people.format('students','teachers','parents')
students,teachers,parents
花括号中的值从0开始
people='{2}'
>>> print people.format('students','teachers','parents')
parents
花括号中的标号,和format中的变量是对应的
- python的基本输入输出
- Python的基本输入输出
- Python的基本输入输出函数
- python 基本输入输出函数
- Python(基本输入输出)
- Python入门--基本输入输出
- python基本输入输出代码示例
- python学习之基本输入输出
- 我的Python学习之路之基本语法-输入输出
- 基本的输入输出
- 基本的输入输出
- C++基本的输入输出
- Java的基本输入输出
- C++ 基本的输入输出
- python的输入输出方式
- python输入输出的处理
- 2.Python的输入输出
- python的标准输入输出
- 8种CSS清除浮动的方法优缺点分析
- bzoj 4448: [Scoi2015]情报传递
- leetcode——15——3Sum
- gitlab集成redmine后,代码push的pre-receive脚本
- Struts2+jquary ajax方式返回json并解析
- python的基本输入输出
- Windows 下的免费 SSH 客户端工具
- Linux - 常用命令
- poj1852Ants
- srand((unsigned)(time(NULL)))的用法
- java开发之反射
- 常用SVN命令行
- C语言-串
- matlab神经网络工具箱创建神经网络