Teifion
Teifion

Reputation: 111059

Shorthand adding/appending in Python

I like that in PHP I can do the following

$myInteger++;
$myString += 'more text';

With Python I must do the following

myInteger = myInteger + 1
myString = myString + "more text"

Is there a better way to add or append to a variable in Python?

Upvotes: 15

Views: 23613

Answers (2)

hyperboreean
hyperboreean

Reputation: 8343

You could do it in the same way you are doing it in PHP:

var += 1

But my advice is to write it down clear:

var = var + 1

Upvotes: 3

Paige Ruten
Paige Ruten

Reputation: 176743

Python doesn't have the increment (++) and decrement (--) operators, but it does have the += operator (and -=, etc.) so you can do this:

myInteger += 1
myString += "more text"

Upvotes: 37

Related Questions