0

I'm having a problem when trying to create a new variable within python.

i = datetime.datetime.now()
tme = i.strftime('%d/%m/%Y')
print tme

This portion of the code creates a date variable (tme) in the format, 10/02/2015. It functions properly.

The second portion will create a expression variable.

exp = "DATE = '"tme"'"
print exp

This portion does not function properly. The problem occurs when I enter the tme variable.

The result I want python to output is a variable which prints:

DATE = '10/02/2015'

2 Answers 2

2

It looks like your code should work other than I think you just forgot a few + signs. Try:

exp = "DATE = '" + tme + "'"

If you have two strings next to each other Python will auto concatenate them, but it will not auto concatenate a string and a variable, even if that variable is a string. Example:

a = 'Hello ''World'
print a

should print Hello World

But the following would NOT work and would result in a syntax error like you are getting:

b = 'World'
a = 'Hello 'b
0
exp = "DATE = '" + tme + "'"

It was that simple!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.