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-"

2
  • 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. Commented Mar 11, 2021 at 19:49
  • 1
    The usual way is s = s[0:5] + '-' + s[6:] Commented Mar 11, 2021 at 19:56

6 Answers 6

3

This is a hack but works:

def replace_at_index(string, index):
   ls = list(string)
   ls[index] = "-"
   s = "".join(ls)
   print(s)
Sign up to request clarification or add additional context in comments.

1 Comment

That's not a hack at all, that's a perfectly fine implementation.
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

Comments

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

Comments

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 ;)

4 Comments

Strings are immutable.
Yep. Long time programming in C led to this
Using += on strings is actually not super Pythonic, but either way, it's simpler to join all at once: new_string = string[:index] + '-' + string[index+1:]
By pythonic i meant by using string slices. Thanks for the observation, though :)
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.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.