+ 1
How to insert element in string just like append function that we use in case of dictionary in python??
2 Answers
+ 6
You can't, strings are immutable in python.
If you just want to concatenate two or more strings, you can simply "add" them:
string1 += string2
string3 = string1 + string2
For a more complex insert function you can create your own function:
def insertIntoString(s, ins, pos):
''' inserts <ins> in position <pos> into string <s> '''
return s[:pos] + ins + s[pos:]
s = 'Hllo'
print(insertIntoString(s, 'e', 1))
>>> Output: Hello
+ 1
prateek verma I did not understand your question.
Are you talking about concatenation?
The dictionary object, in Python, doesn't contain an append method.