I have some string and I want add small part to it. How can I achieve this in python?
string =ass34_ssa ass56_ssa ass78_ssa,ass93_ssa
for i in string :
i.replace("",",")
#i want the string to look like:
string =ass34_ssa,DER_ass56_ssa,DER_ass78_ssa,DER_ass93_ssa
CodePudding user response:
string = "ass34_ssa ass56_ssa ass78_ssa,ass93_ssa"
parts = string.split()
# if , is in the string, then split on that
for i in range(len(parts)):
if "," in parts[i]:
p1, p2 = parts[i].split(",")
# remove the old part
parts.pop(i)
# insert the new parts
parts.extend([p1, p2])
new_string = ""
for part in parts:
if "34" not in part:
new_string = "DER_" part
if part != parts[-1]:
new_string = ","
else:
new_string = part ","
print(new_string)
First, divide the strings into parts if there is a comma in string then further divide that part and add to parts.
2nd, You can add any logic in the 2nd for loop like this if "34" not in part.
Does this answer your question?
