This may be easier than i am making it. Using Model Builder, I have a variable input "Name", which is set as a model parameter, used for a 'Select by Attribute' early in the model. I would like to then use this variable as an input into the final feature class output's name. My issue is that some of these "Names" have spaces in them (E.G. Name = "Watershed 1"). Is there a way, using Model Builder, that i can replace the spaces with underscores/remove the spaces before inserting the variable into the output's name?

Resulting in (....spatialjoin_dissolve_Watershed_1)

Since this model will be run individually for multiple areas i need the name of the watershed to be included, without having to have the user enter in the watershed name with underscores as a parameter.

Thanks for any help!

2 Answers 2


You can do this using the Calculate Value tool to modify the "Name" string variable to replace spaces with underscores. When you are saving the Spatial Join feature class output, you can then use inline variable substitution:


....where 'output_value' is the output name from the Calculate value tool.

Here is what the parameters of the Calculate Value could look like:



....where 'StringInput' is the name of your 'Name' model parameter

Code Block:

def ReplaceName(name) :
  return name.replace(" ", "_")

Data type:


Here is a quick example in ModelBuilder:

enter image description here

Full Size Image

  • 1
    This fits very easily into my model. Very Helpful, Thanks. Commented Apr 9, 2014 at 20:20

The easiest way to do this would be to use the arcpy.ValidateTableName(fc, out_workspace) function, which will automatically convert any invalid characters (like spaces) into valid characters (underscores).

  • Esri Forum post for using this function in the Calculate Value tool in Modelbuilder.
    – Adam
    Commented Apr 8, 2014 at 17:50

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.