在Python中,`text`通常指的是字符串类型(`str`),它是用于存储和处理文本数据的基本数据类型。字符串是由Unicode字符组成的有序序列,可以使用单引号(`'`)、双引号(`"`)或三引号(`'''` 或 `"""`)来创建。
创建字符串:
```python
s1 = 'hello'
s2 = "world"
s3 = '''This is a
multiline
string.'''
访问字符串中的字符:
```python
print(s1) 输出 'h'
修改字符串中的内容:
```python
s1 = s1.replace('h', 'H')
print(s1) 输出 'Hello'
字符串连接:
```python
s4 = s1 + ' ' + s2
print(s4) 输出 'Hello world'
字符串比较:
```python
print('hello' == 'world') 输出 False
字符串搜索:
```python
print('world' in s1) 输出 False
Python的字符串类型非常灵活,支持丰富的操作和方法,使得文本处理变得简单而高效