+ 1

How to insert element in string just like append function that we use in case of dictionary in python??

28th Aug 2018, 5:16 PM
prateek verma
prateek verma - avatar
2 Antworten
+ 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
28th Aug 2018, 5:28 PM
Anna
Anna - avatar
+ 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.
28th Aug 2018, 5:43 PM
Ulisses Cruz
Ulisses Cruz - avatar