11

how do you use multiple %s in a python output?

TEXT = 'Hi, your first name is %s' %Fname

This works fine but...

TEXT = 'Hi, your first name is %s and your last name is %s' %Fname %Lname

I get the error

TypeError: not enough arguments for format string

6 Answers 6

33

Use a tuple:

TEXT = 'Hi, your first name is %s and your last name is %s' % (Fname, Lname)

Better: use str.format(*args, **kwargs).

"Hi, your first name is {0} and your last name is {1}".format("foo", "bar")
Sign up to request clarification or add additional context in comments.

Comments

3

You need to use tuples:

TEXT = 'Hi, your first name is %s', % (Fname)
TEXT = 'Hi, your first name is %s and your last name is %s' % (Fname, Lname)

Also, consider using format().

Comments

1

I like using string format instead.

So

TEXT = 'Hi, your first name is {} and your last name is {}'.format(Fname, Lname)

Comments

1
TEXT = 'Hi, your first name is %s and your last name is %s' %(Fname, Lname)

or

TEXT ='Hi, your first name is {} and your last name is {}'.format(Fname, Lname)

2 Comments

Both is not valid Python.
Syntex mistake in ", %(Fname, Lname)" and right one is %(Fname, Lname)
1

Give all the variables in a tuple, like this:

TEXT = 'Hi, your first name is %s and your last name is %s.' %(Fname,Lname)

Comments

0

If it is print:

print ('Hi, my name is %s ,i %s' % (objectName, targetName))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.