我想随机大写或小写字符串中的每个字母.我是新手在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'对象不支持项目分配
但那么我怎么能这样做呢?