Manipulating Symbols and Contexts by Name

Symbol["name"]construct a symbol with a given name
SymbolName[symb]find the name of a symbol

Converting between symbols and their names.

Here is the symbol .
Its name is a string.
This gives the symbol again.

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

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

The names of the symbols and are the strings and .
This assigns a value to .
Click for copyable input
Whenever you enter it is now replaced by .
The name is not affected, however.
NameQ["form"]test whether any symbol has a name which matches form
Names["form"]give a list of all symbol names which match form
Contexts["form`"]give a list of all context names which match form

Referring to symbols and contexts by name.

and are symbols that have been created in this Wolfram Language session; is not.
Click for copyable input

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

This gives a list of all symbol names in this Wolfram Language session that begin with .
These names correspond to builtin functions in the Wolfram Language.
This asks for names "close" to .
Click for copyable input
Clear["form"]clear the values of all symbols whose names match form
Clear["context`*"]clear the values of all symbols in the specified context
Remove["form"]remove completely all symbols whose names match form
Remove["context`*"]remove completely all symbols in the specified context

Getting rid of symbols by name.

This clears the values of all symbols whose names start with .
Click for copyable input
The name is still known, however.
Click for copyable input
But the value of has been cleared.
Click for copyable input
This removes completely all symbols whose names start with .
Click for copyable input
Now not even the name is known.
Click for copyable input
Remove["Global`*"]remove completely all symbols in the context

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 context. You can remove these symbols completely using Remove["Global`*"]. Builtin Wolfram Language objects are in the context, and are thus unaffected by this.

Translate this page: