Python学习笔记之Python中input函数详解

input()以字符串的方式获取用户输入:

1 >>> x = input()
2 4.5
3 >>> type(x)
4 <class 'str'>
5 >>> y = input()
6 Do you love python?
7 >>> type(y)
8 <class 'str'>
输入的字符串可以通过运算符进行连接、复制等操作:

1 >>> x = input()
2 abc
3 >>> x * 3
4 'abcabcabc'
5 >>> y = input()
6 123
7 >>> x + y
8 'abc123'
但无法直接参与算术运算,如:

 1 >>> x = input()
 2 5
 3 >>> x + 5
 4 Traceback (most recent call last):
 5   File "<stdin>", line 1, in <module>

 6 TypeError: must be str, not int
 7 >>> x * 5
 8 '55555'
 9 >>> y = input()
10 6
11 >>> x * y
12 Traceback (most recent call last):
13   File "<stdin>", line 1, in <module>
14 TypeError: can't multiply sequence by non-int of type 'str'

此时可以使用转换,方法有多种:

1.指定类型转换

1 >>> y = int(input())
2 10
3 >>> type(y)
4 <class 'int'>
 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值