Clarification on the extra use of % in grand finale of date+time in one print


#1

As a newbie I struggled and used Q&A to understand what I was doing wrong, and I somewhat forgot to use (+) to combine the string sets. But what I can't understand is the use of the two extra % in the code? The instructions throw you off a little bit because it asks you to ensure there is only six ( % ) , below is the correct code for grand finale for date and time in one line print. My question after the first set of %s there is a lone % and another one in the end of the second set. What is that % for ?

from datetime import datetime
now = datetime.now()

print '%s/%s/%s ' % (now.month, now.day, now.year) + '%s:%s:%s' % (now.hour, now.minute, now.second)


#2

It's simply the operator that tells Python there is a data tuple following to fill the template.


#3

this is only part of the syntax.
do you understand the + there? + does not mean print + character but it means something special which is add the string after + to the before one. same with %, only it means that after % program will expect variables you want to place in %s positions in the text before.
the clue is the ' ' quotes characters. everything between ' ' is treated as string, outside of ' ' is treated as variable or special character.


#4

thanx for the clarification


#5

Hi

Thanks I sort of got it. So '___' everything between is string and everything outside variable or special character. Based on what you said , that particular lesson wasn't necessarily trying teach me, what you were saying right?
:kissing_heart:


#6

I think that particular lesson was trying to teach you what is the meaning of % special character in print command and why there are %s inside of ' ' and outside of ' ' :slight_smile:


#7

A post was split to a new topic: String formatting


#8