Replacing instances of a character in a string
这个简单的代码仅尝试用冒号替换分号(在i指定的位置)不起作用:
1 2 3 | for i in range(0,len(line)): if (line[i]==";" and i in rightindexarray): line[i]=":" |
它给出了错误
1 2 | line[i]=":" TypeError: 'str' object does not support item assignment |
如何解决此问题,以冒号代替分号? 使用replace不起作用,因为该函数不使用索引-可能有一些我不想替换的分号。
例
在字符串中,我可能有任意多个分号,例如" Hei der!; Hello there;!;"。
我知道我要替换的对象(我在字符串中有索引)。 使用替换无法正常工作,因为我无法对其使用索引。
python中的字符串是不可变的,因此您不能将它们视为列表并分配给索引。
使用
1 | line = line.replace(';', ':') |
如果您只需要替换某些分号,则需要更具体。您可以使用切片来分隔要替换的字符串部分:
1 | line = line[:10].replace(';', ':') + line[10:] |
这将替换字符串的前10个字符中的所有分号。
如果您不想使用
1 2 3 4 5 6 7 8 | word = 'python' index = 4 char = 'i' word = word[:index] + char + word[index + 1:] print word o/p: pythin |
把字符串变成一个列表;那么您可以单独更改字符。然后,您可以将其与
1 2 3 4 5 6 7 | s = 'a;b;c;d' slist = list(s) for i, c in enumerate(slist): if slist[i] == ';' and 0 <= i <= 3: # only replaces semicolons in the first part of the text slist[i] = ':' s = ''.join(slist) print s # prints a:b:c;d |
如果要替换单个分号:
1 2 3 | for i in range(0,len(line)): if (line[i]==";"): line = line[:i] +":" + line[i+1:] |
Havent对此进行了测试。
您不能简单地为字符串中的字符分配值。
使用此方法替换特定字符的值:
1 2 | name ="India" result=name .replace("d",'*') |
输出:In * ia
另外,如果您要为第一个字符以外的所有第一个字符替换说*,
例如。字符串=混音输出= ba ** le
码:
1 2 3 4 5 | name ="babble" front= name [0:1] fromSecondCharacter = name [1:] back=fromSecondCharacter.replace(front,'*') return front+back |
这应该涵盖了一般情况,但是您应该能够针对自己的目的对其进行自定义
1 2 3 4 5 6 7 8 9 10 11 | def selectiveReplace(myStr): answer = [] for index,char in enumerate(myStr): if char == ';': if index%2 == 1: # replace ';' in even indices with":" answer.append(":") else: answer.append("!") # replace ';' in odd indices with"!" else: answer.append(char) return ''.join(answer) |
希望这可以帮助
在字符串上有效使用.replace()方法,而无需创建单独的列表
例如,看看包含有一些空格的字符串的列表用户名,我们要在每个用户名字符串中用下划线替换空格。
1 | usernames = ["Joey Tribbiani","Monica Geller","Chandler Bing","Phoebe Buffay"] |
要替换每个用户名中的空格,请考虑在python中使用range函数。
1 2 3 4 | for i in range(len(usernames)): usernames[i] = usernames[i].lower().replace("","_") print(usernames) |
这个怎么样:
1 2 3 4 5 6 7 8 9 10 11 12 | sentence = 'After 1500 years of that thinking surpressed' sentence = sentence.lower() def removeLetter(text,char): result = '' for c in text: if c != char: result += c return text.replace(char,'*') text = removeLetter(sentence,'a') |
如果要替换为变量" n"中指定的索引值,请尝试以下操作:
1 2 3 | def missing_char(str, n): str=str.replace(str[n],":") return str |
1 2 3 4 5 6 7 8 | names = ["Joey Tribbiani","Monica Geller","Chandler Bing","Phoebe Buffay"] usernames = [] for i in names: if"" in i: i = i.replace("","_") print(i) |
输出:
乔伊·特里比亚尼
莫妮卡·盖勒
钱德勒
菲比·布菲
你可以这样做:
1 2 3 4 5 6 7 8 | string ="this; is a; sample; ; python code;!;" #your desire string result ="" for i in range(len(string)): s = string[i] if (s ==";" and i in [4, 18, 20]): #insert your desire list s =":" result = result + s print(result) |
我写了这种方法来替换字符或替换特定实例的字符串。实例从0开始(如果将可选的inst参数更改为1,并将test_instance变量更改为1,则可以轻松将其更改为1。
1 2 3 4 5 6 7 8 9 10 11 12 13 | def replace_instance(some_word, str_to_replace, new_str='', inst=0): return_word = '' char_index, test_instance = 0, 0 while char_index < len(some_word): test_str = some_word[char_index: char_index + len(str_to_replace)] if test_str == str_to_replace: if test_instance == inst: return_word = some_word[:char_index] + new_str + some_word[char_index + len(str_to_replace):] break else: test_instance += 1 char_index += 1 return return_word |
要替换特定索引处的字符,功能如下:
1 2 3 4 | def replace_char(s , n , c): n-=1 s = s[0:n] + s[n:n+1].replace(s[n] , c) + s[n+1:] return s |
其中s是字符串,n是索引,c是字符。