+= Meaning

I was wondering if someone could clarify what += is and does in Python

it’s a short-hand to add or append. so for example

a=1
a += 1
print(a)
#prints 2

some_string = 'hi '
some_string += 'Robert'
print(some_string)
#prints hi Robert


the reverse works with -=, hmm but I don’t think that works with strings.

2 Likes

The += operator can also be used to extend a list, for example:

alpha = ["α", "β", "γ"]
alpha += ["δ", "ε"]
print(alpha)

Output:

['α', 'β', 'γ', 'δ', 'ε']
1 Like

Well what i would like to say is it is add and append
a = 1
a += 1
means a = a + 1
which again means a = 1 + 1 = 2
which showed up the add property

Now for appending strings
a = hi
a += Robert
which will print or return based on what you want a= hi Robert

Now for time being consider concatenation we can add or append strings but is there such things with string where we can delete … absolutely nope and hence - wont work with string
although try * this for strings…