python 字符串位运算
时间: 2023-08-07 09:44:05 浏览: 131
Python 中的字符串不支持位运算,因为字符串是不可变类型。位运算只能用于整数类型和布尔类型。如果需要对字符串进行位运算,需要先将字符串转换为整数类型。可以使用 `ord()` 函数将单个字符转换为 ASCII 码,然后进行位运算。例如,将字符串中的每个字符取反可以使用以下代码:
```python
str = "hello world"
result = ""
for char in str:
result += chr(~ord(char))
print(result)
```
输出结果为:
```
\n
```
阅读全文
相关推荐


















