小编use*_*472的帖子

在字符串中逐个字符,并使用python交换空格

好的,所以我必须切换''到*s.我想出了以下内容

def characterSwitch(ch,ca1,ca2,start = 0, end = len(ch)):
    while start < end:
        if ch[start] == ca1:
            ch[end] == ca2
        start = start + 1
sentence = "Ceci est une toute petite phrase."
print characterSwitch(sentence, ' ', '*')
print characterSwitch(sentence, ' ', '*', 8, 12)
print characterSwitch(sentence, ' ', '*', 12)
print characterSwitch(sentence, ' ', '*', end = 12)
Run Code Online (Sandbox Code Playgroud)

分配len(ch)似乎不起作用,而且我很确定这不是最有效的方法.以下是我的目标输出:

Ceci*est*une*toute*petite*phrase.
Ceci est*une*toute petite phrase.
Ceci est une*toute*petite*phrase.
Ceci*est*une*toute petite phrase.
Run Code Online (Sandbox Code Playgroud)

python string whitespace

5
推荐指数
1
解决办法
44
查看次数

为什么'abc'[ - 1]导致'c'而不是'b'?

我认为它从一开始就向后计数,所以A为0,B为-1,C为-2.

python string

0
推荐指数
1
解决办法
63
查看次数

标签 统计

python ×2

string ×2

whitespace ×1