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


closed as off-topic by m_goldberg, Carl Lange, Alex Trounev, MarcoB, LCarvalho May 13 at 11:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Carl Lange, Alex Trounev, MarcoB, LCarvalho
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ Print["blah blah is ", 5]? $\endgroup$ – MassDefect May 11 at 0:24
  • $\begingroup$ Wait, why is this off topic? $\endgroup$ – Ion Sme May 15 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 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


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.

  • 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 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 at 6:51

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