相关疑难解决方法(0)

更改字符串中的一个字符?

Python中替换字符串中字符的最简单方法是:

text = "abcdefg";
text[1] = "Z";
           ^
Run Code Online (Sandbox Code Playgroud)

python string

345
推荐指数
9
解决办法
67万
查看次数

随机大写字母串中的字母

我想随机大写或小写字符串中的每个字母.我是新手在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'对象不支持项目分配

但那么我怎么能这样做呢?

python lowercase python-3.x uppercase

14
推荐指数
2
解决办法
1709
查看次数

标签 统计

python ×2

lowercase ×1

python-3.x ×1

string ×1

uppercase ×1