I have a string like: project = '1,2' and I want to insert another number to this string. So for example after the insertion I want to have : project = '1,2,3' Could you please help me?
Thanks
Another option is str.join():
project = '1,2'
n = 3
project = ','.join((project, str(n)))
.format should help you.
project = "1,2"
project += ',{}'.format(3)
And if you have a position, you could use slicing :
project = "1,2,4,5"
position = 3
project = project[:position] + ',3' + project[position:]
EDIT
I thought about another solution:
project += '%s%s' % (',',3)
You can convert it to a list with numbers = project.split(","). You can then add another number with numbers.append('3'). Then make a new string with newproject = ",".join(numbers). Full code:
project = "1,2"
numbers = project.split(",")
numbers.append('3')
newproject = ",".join(numbers)