您的位置:首页 > 编程语言 > Python开发

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%
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐