input函数python怎么用文件_python3下的input函数怎么用

3b1f721fa8dfe4a94975dbd1dd80175d.png

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

4.5

>>> type(x)

>>> y = input()

Do you love python?

>>> type(y)

输入的字符串可以通过运算符进行连接、复制等操作:>>> x = input()

abc

>>> x * 3

'abcabcabc'

>>> y = input()

123

>>> x + y

'abc123'

(更多学习内容,请点击云海天教程网)

但无法直接参与算术运算,如:>>> x = input()

5

>>> x + 5

Traceback (most recent call last):

File "", line 1, in 

TypeError: must be str, not int

>>> x * 5

'55555'

>>> y = input()

6

>>> x * y

Traceback (most recent call last):

File "", line 1, in 

TypeError: can't multiply sequence by non-int of type 'str'

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

1.指定类型转换1 >>> y = int(input())

2 10

3 >>> type(y)

2.自动转换

函数eval() 用来执行一个字符串表达式,并返回表达式的值eval(expression, globals[ ], locals[ ])

global 和 locals 分别相当于全局和局部变量,eval函数会优先在局部变量存储空间中检索1  >>> y = eval(input())

2  4.5

3  >>> type(y)

3.切割转换

利用函数split()通过指定分隔符对字符串进行切片。str.split(str="", num=string.count(str))

str为分割符,包括空格、

, 等 ,num是分割次数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值