-2

I need to replace the first substring in a string with a new string.

For example:

import string
message = 'First part' + ';' + 'Second part'
m='101.0.0.1'
message[0]=str.replace (message[0],message[0],m)
print (message)

I want to get an answer of "101.0.0.1;Second part"

but I am getting the following error:

'str' object does not support item assignment.

Can anybody help me? How I can get an answer like "101.0.0.1; Second part"?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • @StephenRauch (hey do notifications work in this case?) I reopened this because OP's misunderstanding goes deeper than how replacing strings works. – Alex Hall Jun 17 '18 at 21:21
  • @AlexHall Really? We need another answer explaining immutability? – Stephen Rauch Jun 17 '18 at 21:23
  • @StephenRauch no, like I said, OP needs several things explained, particularly their misunderstanding of `message[0]`. – Alex Hall Jun 17 '18 at 21:25
  • @StephenRauch I disagree that a question should only ever be answered if it has long term value to other visitors. OP needed help. I gave it. I was not going to let OP struggle for hours wondering why, say, `message = str.replace (message[0],message[0],m)` or `message = message.replace(message[0],m)` didn't work, as that is probably as far as the duplicates would have helped them. I was not going to squeeze the information in my answer below into a comment. If you want those duplicate links here, close the question or put them in a comment. – Alex Hall Jun 17 '18 at 21:38

1 Answers1

0

This code:

message = 'First part' + ';' + 'Second part'

Is exactly equivalent to:

message = 'First part; Second part'

The + is just combining the strings together into one. It is not going to make Python remember each part separately so that message[0] is 'First part'. message[0] is just the first character of the string, so it's 'F'.

The best you can do is:

message = 'First part; Second part'
m='101.0.0.1'
message = message.replace('First part', m)
print(message)

The import string is not needed at all and calling the method directly on str is not something you want to do.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • @ Alex hall Thanks but my problem is still same. I already did which you send me as ans. but now I am sending you small portion program of server and client on which i am trying to replace.I am sending T_message from cleint to sever using socket Client side T_message='req_status' + ";" + '(20,30)' + ";" + '172.168.1.1 + ";" + '45j' T_message_bytes= bytes(T_message,'utf-8') Server side data = conn.recv(1024).decode() data1=data.split(';') ip='101.2.2.2' Old-ip=data[2] data12=data1.replace(Old-ip,ip) Now Please help me how i can replace '172.168.1.1' with new ip '101.2.2.2' – irfan younus Jun 18 '18 at 00:52
  • @irfanyounus (1) don't put `-` in a variable name, as in `Old-ip`, because that's the subtraction operator (2) `data1` is a list and doesn't have a replace method, use the string, i.e. `data12 = data.replace(...)`. – Alex Hall Jun 18 '18 at 08:24