我想随机大写或小写字符串中的每个字母.我是新手在python中使用字符串,但我认为因为字符串是不可变的,所以我不能执行以下操作:
i =0
for c in sentence:
case = random.randint(0,1)
print("case = ", case)
if case == 0:
print("here0")
sentence[i] = sentence[i].lower()
else:
print("here1")
sentence[i] = sentence[i].upper()
i += 1
print ("new sentence = ", sentence)
Run Code Online (Sandbox Code Playgroud)
并得到错误:TypeError:'str'对象不支持项目分配
但那么我怎么能这样做呢?
这就是我在ipython中所做的(我使用的是Python 3.6)
from PyDictionary import PyDictionary
dictionary = PyDictionary()
list = dictionary.synonym("life")
Run Code Online (Sandbox Code Playgroud)
我收到错误:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyDictionary/utils.py:5: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html5lib"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
The code that caused this warning is on line 5 of the file /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyDictionary/utils.py. To get rid of this …Run Code Online (Sandbox Code Playgroud) python 3中是否有可以bitwise-xor字节的内置函数?例如,如果我有 2 个字节数组:
one = oE1ltQSsoEqRC4j1EMz1ORU1dyucIcI4WstKz-uhuKA=
two = Rffs1PW5zA1h5RFVh5MkLw5R7a2QVHY7cwnjuSPktwc=
one XOR two = 5bqJYfEVbEfw7pmgl1_RFhtkmoYMdbQDKcKpdshFD6c=
Run Code Online (Sandbox Code Playgroud)