用Python函数将字母A、B、C、D用数字1、2、3、4进行批量替换
时间: 2023-09-27 12:04:56 浏览: 48
可以使用Python中的replace函数实现批量替换,示例如下:
```python
def replace_letters_with_numbers(text):
text = text.replace('A', '1').replace('B', '2').replace('C', '3').replace('D', '4')
return text
```
其中,replace函数可以将字符串中指定的字符替换为另一个字符。使用该函数进行批量替换时,可以利用链式调用将多个替换操作连接起来,实现一次性替换。
使用示例:
```python
text = 'ACBD'
text = replace_letters_with_numbers(text)
print(text) # 输出 '1342'
```