Python文本处理全解析:从字符串基础到正则表达式
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 语句查看变量的内容,与直接在解释器中输入变量名不同, print 语句不会显示引号。示例如下:
>>> monty = 'Monty Python'
>>> pri
超级会员免费看
订阅专栏 解锁全文
1128

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



