Python文本处理:字符串、Unicode与正则表达式
1. 字符串基础操作
1.1 数据类型混淆错误
在Python中,对字符串进行不恰当的操作会导致数据类型混淆错误。例如:
>>> 'very' - 'y'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> 'very' / 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'int'
这表明减法操作不能用于字符串类型,除法操作也不能以字符串和整数作为操作数。
1.2 打印字符串
查看变量内容或计算结果时,除了直接在解释器中输入变量名,还可以使用 print 语句:
>>> monty = 'Monty Python'
>>> print monty
Monty Python
直接输
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



