7

I am trying to join together strings and variables as part of formatting the output of my code. For example how do I print the line "5 trees" where the number 5 is a variable n that could be changing. I've tried the following, which doesn't work:

 n=5;
 Print[n <> "trees"]

Any idea on how to get that to print as "5 trees"? Thanks!

lwcarani
  • 179
  • 1
  • 5

2 Answers2

9

For trivial combinations, simple Print combinations work well.

For more complex operations, take a look at StringForm:

var1 = "Tree";
var2 = 10;

StringForm["The `1` is about `2` feet tall...", var1, var2]

(* "The Tree is about 10 feet tall..." *)
ciao
  • 25,774
  • 2
  • 58
  • 139
7
n = 5;
Print[ToString[n] <> " trees"]
David G. Stork
  • 41,180
  • 3
  • 34
  • 96