This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
Wolfram Research, Inc.

Automatic Loading of PackagesAdvanced Topic: Intercepting the Creation of New Symbols

2.6.12 Manipulating Symbols and Contexts by Name

Converting between symbols and their names.

Here is the symbol x.

In[1]:= x // InputForm

Out[1]//InputForm= x

Its name is a string.

In[2]:= SymbolName[x] // InputForm

Out[2]//InputForm= "x"

This gives the symbol x again.

In[3]:= Symbol["x"] // InputForm

Out[3]//InputForm= x

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]:= t = {SymbolName[x], SymbolName[xp]} // InputForm

Out[4]//InputForm= {"x", "xp"}

This assigns a value to x.

In[5]:= x = 2


Whenever you enter x it is now replaced by 2.

In[6]:= {x, xp} // InputForm

Out[6]//InputForm= {2, xp}

The name "x" is not affected, however.

In[7]:= t // InputForm

Out[7]//InputForm= InputForm[{"x", "xp"}]

Referring to symbols and contexts by name.

x and xp are symbols that have been created in this Mathematica session; xpp is not.

In[8]:= {NameQ["x"], NameQ["xp"], NameQ["xpp"]}


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

This gives a list of all symbol names in this Mathematica session that begin with x.

In[9]:= Names["x*"] // InputForm

Out[9]//InputForm= {"x", "xp"}

These names correspond to built-in functions in Mathematica.

In[10]:= Names["Qu*"] // InputForm

Out[10]//InputForm= {"Quartics", "QuasiMonteCarlo", "QuasiNewton", "Quit", "Quotient"}

This asks for names "close" to WeierstrssP.

In[11]:= Names["WeierstrssP", SpellingCorrection->True]


Getting rid of symbols by name.

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

In[12]:= Clear["x*"]

The name "x" is still known, however.

In[13]:= Names["x*"]


But the value of x has been cleared.

In[14]:= {x, xp}


This removes completely all symbols whose names start with x.

In[15]:= Remove["x*"]

Now not even the name "x" is known.

In[16]:= 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 Mathematica session will be placed in the Global` context. You can remove these symbols completely using Remove["Global`*"]. Built-in Mathematica objects are in the System` context, and are thus unaffected by this.

Automatic Loading of PackagesAdvanced Topic: Intercepting the Creation of New Symbols