python基础学习-输入输出

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值