0

I am trying to run the following script with Python:

test_string = "Hallo"

test_string[0] = "T"

And I am getting the following error:

'str' object does not support item assignment

Now I know you can use the replace default function of Python, but is there any other way to achieve the above without using a standard python function ?

thanks,

4
  • 1
    you can create a list of letters. but it's weird Commented Aug 17, 2017 at 14:40
  • 4
    You get the error because strings are immutable. Hence, any solution will just return a new object. However, why do you want to use a non standard function? Is a regex non standard? Are list comprehensions nonstandard? What is bad about replace? Additionally, the question in it's current form is way to broad Commented Aug 17, 2017 at 14:40
  • 1
    test_string = list("Hallo") test_string[0] = 'T' Commented Aug 17, 2017 at 14:42
  • Another non-standard way could be to use a multiclass classifier that classifies the first letter always as T and keeps the remaining letters constant. Then you could predict the class of every letter and concatenate back again. However, this only works for strings of a fixed lenght n Commented Aug 17, 2017 at 18:19

6 Answers 6

4

This works:

test_string = "Hallo"
# turn the string into a list
test_string  = list(test_string)
# change the character you want  
test_string[0] = "T"
# convert the list back to a string. 
test_string = "".join(test_string)
Sign up to request clarification or add additional context in comments.

9 Comments

@PauloAlmeida exactly, my mistake! Thanks
Why test_string = "".join(test_string)?
@haccks to get back to a string as the OP wants to change a "string". i.e we change temporary to a list to change a character then we get back to a string.
test_string = list(test_string) changes test_string in place, i.e. it makes test_string list.
@haccks It makes test_string a list, but not "in place". It creates a new list object from the string test_string and reassigns the name test_string to this new object. I may be missing your point though, are you saying something could be different in this answer or just clarifying?
|
2

Strings are immutable, so if you don't want to convert to a mutable type, you'll have to create a new string. Here is an example function that does that:

def replace_(original_string, replace_string, index):
    return original_string[:index] + replace_string + original_string[index + len(replace_string):]

print(replace_("Hallo", "T", 0))
print(replace_("Hallo", "Te", 0))

This outputs:

Tallo
Tello

I'd like to note that I prefer the answers that convert to a list; this answer is only provided for a pure-string implementation.

Comments

1

So you want to change just the first letter of your string?

You can do

t = "T" + t[1:len(t)]

note: I've put "t" instead of "test_string"

Comments

0

Using a list comprehension and enumerate like

"".join(['T' if i == 0 else x for i, x in enumerate(test_string) ])

also works

Comments

0

Here's another solution using bytearray which is a little faster than MedAli's solution:

test_string = "Hello"
b = bytearray(test_string)  # Or Python3 bytearray(test_string, 'utf-8')
b[0] = ord('T')
str(b)

1 Comment

This won't work if the string has characters that use more than a byte. Also, in Python 3 you have to specify an encoding, but that won't help with the replacement.
0

Strange that nobody did the obvious...

test_string = "T" + test_string[1:]

2 Comments

Someone did, actually, but they unnecessarily added len(test_string) to the slice. It is a bit more cumbersome if the character you want to replace isn't in the beginning or end.
@PauloAlmeida Yeah I saw that. It's not the same.

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.