在Python中,字符串可以通过多种方式进行赋值。以下是一些常见的方法:
直接赋值
```python
string1 = 'hello'
string2 = 'world'
string3 = 'python'
列表推导式
```python
strings = ['hello', 'world', 'python']
string_list = [string for string in strings]
手动赋值
```python
string1 = 'hello'
string2 = 'world'
string3 = 'python'
string_list = [string1, string2, string3]
序列解包
```python
string1, string2, string3 = 'hello', 'world', 'python'
链式赋值
```python
x = y = z = 'hello'
exec()函数
```python
exec('x = 123')
print(x) 输出:123
使用locals()
```python
locals()['W1'] = 123
print(W1) 输出:123
使用exec语句(Python 2)
```python
exec 'W1 = 123'
print W1 输出:123