python基础学习-输入输出
2017-03-03 12:12
501 查看
python的基本输入是input(...)函数,但输入的都是字符串类型的,如果想输入其他类型的数据,就需要把输入的字符串类型的数据进行数据类型强转,比如输入"hello"就可以直接用input(...)直接进行输入
>>> string = input("please input a str:")
please input a str:hello
>>> print("input str is ",string)
input str is hello
而如果想要输入数字10的话就需要这样:
>>> number = input("please input a number:")
please input a number:10
>>> number = int(number)
>>> type(number)
<class 'int'>
>>> print(number)
10
基本输出是用print(...),可以一次性输出多个数据,中间用逗号隔开,比如输入hello world
>>> print("hello ","world")
hello world
然后就是格式化输入,使用format(...)函数
>>> print(format(12.3456,"4.2f"))
12.35
这里的4是占位的宽度,2是精度,如果宽度小于等于精度就左对齐,否则右对齐
>>> print(format(12.3456,"6.1f"))
12.3
百分数格式化输出,格式同上
>>> print(format(0.123456,"6.1%"))
12.3%
>>> string = input("please input a str:")
please input a str:hello
>>> print("input str is ",string)
input str is hello
而如果想要输入数字10的话就需要这样:
>>> number = input("please input a number:")
please input a number:10
>>> number = int(number)
>>> type(number)
<class 'int'>
>>> print(number)
10
基本输出是用print(...),可以一次性输出多个数据,中间用逗号隔开,比如输入hello world
>>> print("hello ","world")
hello world
然后就是格式化输入,使用format(...)函数
>>> print(format(12.3456,"4.2f"))
12.35
这里的4是占位的宽度,2是精度,如果宽度小于等于精度就左对齐,否则右对齐
>>> print(format(12.3456,"6.1f"))
12.3
百分数格式化输出,格式同上
>>> print(format(0.123456,"6.1%"))
12.3%
相关文章推荐
- python 基础学习1---输入输出
- Python3学习笔记【基础2--输出,输入,格式化,编码】
- 黑马程序员之WinForm编程基础学习笔记:输入宽和高,输出面积。
- 递归调用输出元素 分类: python 小练习 python基础学习 2013-06-30 11:54 278人阅读 评论(0) 收藏
- Python基础:数据类型、变量定义、输入/输出、逻辑、函数/模块(导入)/类、异常处理
- Python学习之:输入输出
- Python3基础之输入和输出实例分析
- python基础(文件输入/输出 内建类型 字典操作使用方法)
- 黑马程序员之WinForm编程基础学习笔记:在多行文本框中输入多行"姓名=成绩"格式的数据,要求输出成绩最高的学生的姓名和成绩。
- python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
- Python学习-12.Python的输入输出
- Python学习入门基础教程(learning Python)--1.2.3 Python格式化输出百分比
- 输出包含共同元素的变量值 分类: python 小练习 python基础学习 2014-03-11 13:55 188人阅读 评论(0) 收藏
- Python基础知识(三)--基本的异常处理、算术运算符、输入/输出
- 标准输出stdout、标准错误stderr 分类: python python基础学习 2013-06-17 18:08 308人阅读 评论(0) 收藏
- Python学习入门基础教程(learning Python)--1.2.2 Python格式化输出基础 .
- Python学习入门基础教程(learning Python)--1.3 Python数据输入 .
- python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
- python学习-输入输出
- Python学习笔记:02 基础:变量、整数、字符串、赋值、基本输出、注释、代码块