# Using strings as commands: expr

### 12.4.7 Using strings as commands: expr

The expr command lets you use a string as a command.

- expr takes one argument:
  str, a string which expresses a valid command.
- expr(str) converts str to the command and evaluates it.

#### Examples.

- Input:

  expr("c:= 1")
  c

  Output:

  1

- Input:

  a:= "ifactor(54)"
  expr(a)

  Output:

  2· 33

  which is the same thing as entering ifactor(54) directly.

You can also use expr to convert a string to a number. If a string is simply a number enclosed by quotation marks, then expr will return the number.

#### Example.

Input:

expr("123")

Output:

123

In particular, the following strings will be converted to the appropriate number.

- A string consisting of the digits 0 through 9 which doesn’t start with 0 will be converted to an integer.

  #### Example.

  Input:

  expr("2133")

  Output:

  2133

- A string consisting of the digits 0 through 9 which contains a single decimal point will be converted to a decimal.

  #### Example.

  Input:

  expr("123.4")

  Output:

  123.4

- A string consisting of the digits 0 through 9, possibly containing a single decimal point, followed by e and then more digits 0 through 9, will be read as a decimal in exponential notation.

  #### Example.

  Input:

  expr("1.23e4")

  Output:

  12300.0

- A string consisting of the digits 0 through 7 which starts with 0 will be read as an integer base 8.

  #### Example.

  Input:

  expr("0176")

  Output:

  126

  since 176 base 8 equals 126 base 10.

- A string starting with 0x followed by digits 0 through 9 and letters a through f will be read as an integer base 16.

  #### Example.

  Input:

  expr("0x2a3f")

  Output:

  10815

  since 2a3f base 16 equals 10815 base 10.

- A string starting with 0b followed by digits 0 and 1 will be read as a binary integer.

  #### Example.

  Input:

  expr("0b1101")

  Output:

  13

  since 1101 base 2 equals 13 base 10.