Python文本处理:字符串、Unicode与正则表达式
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.1 打印字符串
我们可以使用 print 语句查看变量的内容。例如:
>>> monty = 'Monty Python'
>>> print monty
Monty Python
当直接在解释器中输入变量名时,解释器会打印其Python表示形式,
超级会员免费看
订阅专栏 解锁全文
4万+

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



