Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 4 down vote accepted

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

share|improve this answer
This fits very easily into my model. Very Helpful, Thanks. – JB_Env_Science_BGE Apr 9 '14 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).

share|improve this answer
Esri Forum post for using this function in the Calculate Value tool in Modelbuilder. – amarinel Apr 8 '14 at 17:50

Your Answer


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.