Is there any function in Python that I can use to insert a value in a certain position of a string?
Something like this:
"3655879ACB6"
then in position 4 add "-"
to become "3655-879ACB6"
martineau
115k25 gold badges160 silver badges284 bronze badges
asked Mar 10, 2011 at 1:32
Michel AndradeMichel Andrade
3,7875 gold badges26 silver badges28 bronze badges
0
No. Python Strings are immutable.
>>> s='355879ACB6'
>>> s[4:4] = '-'
Traceback [most recent call last]:
File "", line 1, in
TypeError: 'str' object does not support item assignment
It is, however, possible to create a new string that has the inserted character:
>>> s[:4] + '-' + s[4:]
'3558-79ACB6'
answered Mar 10, 2011 at 1:34
2
This seems very easy:
>>> hash = "355879ACB6"
>>> hash = hash[:4] + '-' + hash[4:]
>>> print hash
3558-79ACB6
However if you like something like a function do as this:
def insert_dash[string, index]:
return string[:index] + '-' + string[index:]
print insert_dash["355879ACB6", 5]
answered Mar 10, 2011 at 1:39
2
As strings are immutable another way to do this would be to turn the string into a list, which can then be indexed and modified without any slicing trickery. However, to get the list back to a string you'd have to use .join[]
using an empty string.
>>> hash = '355879ACB6'
>>> hashlist = list[hash]
>>> hashlist.insert[4, '-']
>>> ''.join[hashlist]
'3558-79ACB6'
I am not sure how this compares as far as performance, but I do feel it's easier on the eyes than the other solutions. ;-]
answered Mar 10, 2011 at 1:48
jathanismjathanism
32.3k9 gold badges67 silver badges86 bronze badges
0
Simple function to accomplish this:
def insert_str[string, str_to_insert, index]:
return string[:index] + str_to_insert + string[index:]
answered Mar 16, 2017 at 10:26
vatsugvatsug
5245 silver badges15 bronze badges
Python 3.6+ using f-string:
mys = '1362511338314'
f"{mys[:10]}_{mys[10:]}"
gives
'1362511338_314'
amain
1,54812 silver badges19 bronze badges
answered Apr 24, 2020 at 19:14
DarinPDarinP
1251 silver badge2 bronze badges
1
I have made a very useful method to add a string in a certain position in Python:
def insertChar[mystring, position, chartoinsert ]:
longi = len[mystring]
mystring = mystring[:position] + chartoinsert + mystring[position:]
return mystring
for example:
a = "Jorgesys was here!"
def insertChar[mystring, position, chartoinsert ]:
longi = len[mystring]
mystring = mystring[:position] + chartoinsert + mystring[position:]
return mystring
#Inserting some characters with a defined position:
print[insertChar[a,0, '-']]
print[insertChar[a,9, '@']]
print[insertChar[a,14, '%']]
we will have as an output:
-Jorgesys was here!
Jorgesys @was here!
Jorgesys was h%ere!
answered Feb 4, 2016 at 15:13
JorgesysJorgesys
121k23 gold badges322 silver badges259 bronze badges
3
I think the above answers are fine, but I would explain that there are some unexpected-but-good side effects to them...
def insert[string_s, insert_s, pos_i=0]:
return string_s[:pos_i] + insert_s + string_s[pos_i:]
If the index pos_i is very small [too negative], the insert string gets prepended. If too long, the insert string gets appended. If pos_i is between -len[string_s] and +len[string_s] - 1, the insert string gets inserted into the correct place.
answered Feb 7, 2020 at 1:57
Gary02127Gary02127
4,7811 gold badge23 silver badges28 bronze badges
If you need to insert a given char at multiple locations, always consider creating a list of substrings and then use .join[]
instead of +
for string concatenation. This is because, since Python str
are mutable, +
string
concatenation always adds an aditional overhead. More info can be found here.
answered Jul 25 at 22:06
WenukaWenuka
7312 gold badges8 silver badges20 bronze badges
If you want many inserts
from rope.base.codeanalyze import ChangeCollector
c = ChangeCollector[code]
c.add_change[5, 5, '']
c.add_change[10, 10, '']
rend_code = c.get_changed[]
answered May 31, 2015 at 15:27
enomadenomad
9838 silver badges16 bronze badges
1