3
def replace_at_index(string, index):
    print (string.replace(string[index], "-", 1))

That's my current code for replacing a character with a given index.

  • string "House" with Index 4 produces "Hous-"
  • string "Housee" with Index 5 produces "Hous-e"
  • string "Houseed" with Index 6 produces "Housee-"
  • string "Houseed" with Index 5 produces "Hous-ed"

Not sure why it's doing this. The result I'm wanting is for it to replace the given Index, which in the case of "Housee" Index 5 would be "House-"

matt
  • 33
  • 5
  • 5
    It's replacing the first occurrence of the character you asked it to replace. If there are duplicate characters, it won't necessarily be at the same index that you got the character from. – Barmar Mar 11 '21 at 19:49
  • 1
    The usual way is `s = s[0:5] + '-' + s[6:]` – Tim Roberts Mar 11 '21 at 19:56

6 Answers6

3

This is a hack but works:

def replace_at_index(string, index):
   ls = list(string)
   ls[index] = "-"
   s = "".join(ls)
   print(s)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
ESDAIRIM
  • 611
  • 4
  • 12
2

Look at the function doc-string: string.replace(old, new, count) so it replaces count as many occurences that it can find.

You cannot change a string. Each string is newly created, it is immutable. What you want is:

def replace_at_index(string, index):
    string = string[:index] + "-" + string[index+1:]
    return string
skjerns
  • 1,905
  • 1
  • 16
  • 25
1

The replace method replaces a given substring in a string.

What the code is doing, it's replacing the first occurrence of the character in the string.

What you should do instead is:

def replace_at_index(string, index):
    new_string = string[:index]
    new_string += "-"
    new_string += string[index+1:]
    return new_string

In a pythonic fashion ;)

Hiram
  • 143
  • 1
  • 8
1

str.replace is not replacing the index, but the first occurrence of a value. Since "Housee"[5] == 'e', it will replace the first 'e'

def replace_at_index(string, index):
    newstr = string[:index] + '-' + string[index+1:]
    return newstr
trozzel
  • 479
  • 5
  • 12
0

It's replacing the first occurrence of the character you asked it to replace. If there are duplicate characters, it won't necessarily be at the same index that you got the character from.

-- comment by Barmar

What you could do instead is slice the string at the index, then join it on the replacement.

def replace_at_index(string, index):
    parts = string[:index], string[index+1:]
    return "-".join(parts)


for s, i in ("House", 4), ("Housee", 5), ("Houseed", 6), ("Housee", 5):
    print(replace_at_index(s, i))

Output:

Hous-
House-
Housee-
House-

Although you might want to add a check to make sure that index is in range.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

its replacing you just have to be a little smart about it:

b = '?<\>:/"|'
line='?sqfsq?QSqdqdf/qsdfqsdf\qsdfqs"'
s=""
for char in b:
    print(char)
    # if char in b:
    s=(line.replace(char,''))
    line=s
print(line)
print(s)
kilojoules
  • 9,768
  • 18
  • 77
  • 149