字符串连接
使用加号(`+`)运算符可以将两个字符串连接起来。
```python
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) 输出:Hello World
字符串重复
使用星号(`*`)运算符可以重复字符串多次。
```python
str = "Python"
result = str * 3
print(result) 输出:PythonPythonPython
字符串切片
使用方括号(`[]`)可以截取字符串的一部分。
```python
str = "Hello World"
result = str[0:5] 输出:Hello
字符串比较
使用比较运算符(`==`, `!=`, `<`, `>`, `<=`, `>=`)可以比较字符串。
```python
str1 = "Hello"
str2 = "World"
print(str1 == str2) 输出:False
字符串成员运算
使用`in`和`not in`可以检查一个字符串是否包含另一个字符串。
```python
str1 = "Hello"
str2 = "World"
print("H" in str1) 输出:True
print("M" not in str1) 输出:True
字符串格式化
使用`%`格式化字符串,或者`.format()`方法,或者f-string(Python 3.6+)来插入变量到字符串中。
```python
name = "Alice"
age = 30
print("My name is %s and I am %d years old." % (name, age)) 输出:My name is Alice and I am 30 years old.
print(f"My name is {name} and I am {age} years old.") 输出:My name is Alice and I am 30 years old.
以上是Python中字符串运算的基本方法。