s1 = "cats"
print "are 10 cats {}" .format(s1*10)
print "are 10 cats {10cats}" .format(10cats=s1*10)
First print
works, second I get SyntaxError: invalid syntax
. Why?
s1 = "cats"
print "are 10 cats {}" .format(s1*10)
print "are 10 cats {10cats}" .format(10cats=s1*10)
First print
works, second I get SyntaxError: invalid syntax
. Why?
Variable names in python (and many other languages) cannot start with numbers. If you used a legal variable name, the second would work fine.
>>> s1 = "cats"
>>> print("are 10 cats {cats}" .format(cats=s1*10))
are 10 cats catscatscatscatscatscatscatscatscatscats
As stated already, variable names in Python cannot start with numbers. Additionally, there are other characters that cannot be used in variable names in Python.