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 x.
In[1]:=
Click for copyable input
Its name is a string.
In[2]:=
Click for copyable input
This gives the symbol x again.
In[3]:=
Click for copyable input

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".
In[4]:=
Click for copyable input
This assigns a value to x.
In[5]:=
Click for copyable input
Out[5]=
Whenever you enter x it is now replaced by 2.
In[6]:=
Click for copyable input
The name "x" is not affected, however.
In[7]:=
Click for copyable input
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.

x and xp are symbols that have been created in this Wolfram Language session; xpp is not.
In[8]:=
Click for copyable input
Out[8]=

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.
In[9]:=
Click for copyable input
These names correspond to builtin functions in the Wolfram Language.
In[10]:=
Click for copyable input
This asks for names "close" to WeierstrssP.
In[11]:=
Click for copyable input
Out[11]=
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 x.
In[12]:=
Click for copyable input
The name "x" is still known, however.
In[13]:=
Click for copyable input
Out[13]=
But the value of x has been cleared.
In[14]:=
Click for copyable input
Out[14]=
This removes completely all symbols whose names start with x.
In[15]:=
Click for copyable input
Now not even the name "x" is known.
In[16]:=
Click for copyable input
Out[16]=
Remove["Global`*"]remove completely all symbols in the Global` 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 Global` context. You can remove these symbols completely using Remove["Global`*"]. Builtin Wolfram Language objects are in the System` context, and are thus unaffected by this.

Translate this page: