Manipulating Symbols and Contexts by Name

Converting between symbols and their names.

Here is the symbol x:

x//InputForm

Its name is a string:

SymbolName[x]//InputForm

This gives the symbol x again:

Symbol["x"]//InputForm

Once you have made an assignment such as x=2, then whenever x is evaluated, it is replaced by 2. Sometimes, however, you may want to continue to refer to x itself, without immediately getting the value of x.

You can do this by referring to x by name. The name of the symbol x is the string "x", and even though x itself may be replaced by a value, the string "x" will always stay the same.

The names of the symbols x and xp are the strings "x" and "xp":

t={SymbolName[x],SymbolName[xp]}//InputForm

This assigns a value to x:

x=2

Whenever you enter x it is now replaced by 2:

{x,xp}//InputForm

The name "x" is not affected, however:

t//InputForm

Referring to symbols and contexts by name.

x and xp are symbols that have been created in this Wolfram Language session; xpp is not:

{NameQ["x"],NameQ["xp"],NameQ["xpp"]}

You can specify the form of symbol names using string patterns of the kind discussed in "String Patterns". "x*" stands, for example, for all names that start with x.

This gives a list of all symbol names in this Wolfram Language session that begin with x:

Names["x*"]//InputForm

These names correspond to built-in functions in the Wolfram Language:

Names["Qu*"]//InputForm

This asks for names "close" to WeierstrssP:

Names["WeierstrssP",SpellingCorrection->True]

Getting rid of symbols by name.

This clears the values of all symbols whose names start with x:

Clear["x*"]

The name "x" is still known, however:

Names["x*"]

But the value of x has been cleared:

{x,xp}

This removes completely all symbols whose names start with x:

Remove["x*"]

Now not even the name "x" is known:

Names["x*"]

Removing all symbols you have introduced.

If you do not set up any additional contexts, then all the symbols that you introduce in a Wolfram Language session will be placed in the Global` context. You can remove these symbols completely using (RefLink[Remove, paclet:ref/Remove])["Global`*"]. Built-in Wolfram Language objects are in the System` context, and are thus unaffected by this.