python基础学习-输入输出

来源:互联网 发布:网络大电影如何赚钱 编辑:程序博客网 时间:2024/05/17 23:35

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%

0 0