Nishant Singh
Nishant Singh

Reputation: 3209

Python string format error

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?

Upvotes: 1

Views: 6744

Answers (6)

zondo
zondo

Reputation: 20336

% 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.

Upvotes: 1

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23203

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.

Upvotes: 3

Sam Ireland
Sam Ireland

Reputation: 552

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.

Upvotes: 0

cenh
cenh

Reputation: 164

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.

Upvotes: 0

Vikas Madhusudana
Vikas Madhusudana

Reputation: 1482

because formatter expects a tuple.

Upvotes: 0

imrek
imrek

Reputation: 3040

>>> 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.

Upvotes: 0

Related Questions