I have a problem updating a map element text (named element1) in a map document using the following code:

a1 = '"' + ("element" + str(1)) + '"'
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if elm.name  == a1:
    elm.text = str("new text")
    elm.elementPositionX = 0.6046
    elm.elementPositionY = 6.4636

The code above does not update the element1 with the new text value.

It looks like the following part does not work:

if elm.name  == a1:

Because when using the below hard coded approach:

if elm.name  == "element1":

It worked, but I need to have it as variable.

What am I doing wrong here?

closed as too localized by blah238, Mike T, whuber Feb 25 '13 at 14:54

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • replace <<a1 = '"' + ("element" + str(1)) + '"'>> by a1 = "element" + str(1) , should work – geogeek Feb 24 '13 at 23:13
  • 1
    'element1' == "element1" != '"element1"' – Mike T Feb 25 '13 at 3:11
  • many thanks for your help - the geogeek's hint worked a trick for me! and thanks Mike - all understood :) – meryloo Feb 25 '13 at 8:15

You don't need to wrap the element name in quotes like you are doing.

You can use string formatting to do this slightly more cleanly:

a1 = "element{0}".format(1)

See also my suggestions on general Python tips in this answer.

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