Is there something similar to print("blah blah is %d", 5) in python? In order to print "blah blah is 5"?

  • 3
    $\begingroup$ Print["blah blah is ", 5]? $\endgroup$ – MassDefect May 11 '19 at 0:24
  • $\begingroup$ Wait, why is this off topic? $\endgroup$ – Ion Sme May 15 '19 at 3:28
  • $\begingroup$ In python doing comma splicing is seen as bad practice, so wanted to avoid Print["blah blah is ", 5] That's why I asked the question. $\endgroup$ – Ion Sme May 15 '19 at 3:42

You can use StringForm:

StringForm["blah blah is `1` and other blah is `2`", 5, 10]

blah blah is 5 and other blah is 10

| improve this answer | |

There's also StringTemplate:

StringTemplate["blah blah blah is `first`"][<|"first"->1|>]
blah blah blah is 1

which has the advantage that you can assign your template and easily call it frequently (here, I'm using a simpler form than above):

template = StringTemplate["blah blah blah is ``"]

Map[template, {1,2,3,4}]
{"blah blah blah is 1", "blah blah blah is 2", "blah blah blah is 3", "blah blah blah is 4"}

You might then use Print or Echo or similar functions to actually "print" your statements, but this isn't a common paradigm in Wolfram Language.

| improve this answer | |
  • 1
    $\begingroup$ I don't like StringTemplate because of things like: StringTemplate["blah blah blah is `first`"][<|"first"->1/2|>]. $\endgroup$ – Carl Woll May 11 '19 at 22:28
  • $\begingroup$ @CarlWoll What behaviour would you prefer? I feel as though in python, print('x, y, %d', 1/2) would also print 'x, y, 0.5'... But in any case, I only wanted to add it as an option here. StringForm is probably closer to what the user wanted :) $\endgroup$ – Carl Lange May 12 '19 at 6:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.