1

This is pretty basic:

my_name="nishant"
my_age=24
print "My name is %s"%my_name
print "My age is %d & my name is %s, what are you ?" %(my_age, my_name)

It works fine and prints the intended result, but if I replace the last line as this:

print "My age is %d & my name is %s, what are you ?" %my_age , %my_name

I get this error:

File "my_tests.py", line 7
  print "My age is %d & my name is %s, what are you ?" %my_age, %my_name
                                                                  ^
SyntaxError: invalid syntax

My question is :

  1. Why is %(my_age, my_name) != %my_age , %my_name?
  2. How does Python interpret something like this?

6 Answers 6

3

What's performed by your snippet is binary arithmetic operation, and it require single object as second argument.

With parenthesis, you defining this argument as a two-element tuple. I added additional parenthesis to emphasise how code is interpreted.

print ("My age is %d & my name is %s, what are you ?" % (my_age, my_name))

When not, argument is single element and , %my_name is interpreted as second argument to print statement.

print evaluates each expression in turn and writes the resulting object to standard output

print ("My age is %d & my name is %s, what are you ?" % my_age), (%my_name)

Since %my_name is invalid Python expression, SyntaxError is raised.

Sign up to request clarification or add additional context in comments.

1 Comment

this kinda makes sense to me
1

% is an operator just like +, -, /, *, &, |, etc. Just as you can't do 4 * 5, * 6, you can't do '%s %s' % 'here', % 'there'. Actually, x % y is just a shortcut for x.__mod__(y)1. Therefore,

'%s %s' % ('here', 'there') -> '%s %s'.__mod__(('here', 'there'))

Using % twice just doesn't make sense:

'%s %s'.__mod__('here'), .__mod__('there')

1 or y.__rmod__(x) if x.__mod__() doesn't exist.

Comments

0
>>> print "My name is %s & my age is %d, what are you?" % (my_name, my_age)
My name is nishant & my age is 24, what are you?

After the % sign you pass in a tuple of the variables you need.

Sorry, I misinterpreted your question in my previous answer.

Comments

0

because formatter expects a tuple.

2 Comments

you meant if you want multiple formats in your string to print multiple variables, you need to put them inside ( ) (parentheses) separated by , (commas). ?
yes a % and tuple containing number of variables equal to % in string
0

It's the python syntax for strings, you can read the python documentation for how a conversion specifier with more than two variables work:

https://docs.python.org/2/library/stdtypes.html#string-formatting-operations

It's just a design solution, that the developers thought was best. Python expects a tuple, when there is more than one variable.

2 Comments

@NishantSingh You have two values, need to wrap them as a tuple.
Because, that's how the developers of Python wanted it to be. It's a design solution for putting variables inside a string.
0

The % operator should only appear once - it is not part of the variable name. This is how you would usually see it used:

print "My age is %d & my name is %s, what are you ?" % (my_age, my_name)

The space between the % and the tuple is just to emphasise their distinctness.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.