Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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'

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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
share|improve this answer
exp = "DATE = '" + tme + "'"

It was that simple!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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