Let's say I have an extremely long string with arguments that I want to create. I know you can create a multiline string with
cmd = """line 1 line 2 line 3"""
But now lets say I want to pass 1, 2, and 3 as arguments.
This works
cmd = """line %d line %d line %d""" % (1, 2, 3)
But if I have a super long string with 30+ arguments, how can I possibly pass those arguments in multiple lines? Passing them in a single line defeats the purpose of even trying to create a multiline string.
Thanks to anyone in advance for their help and insight.
Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.
First of all, Java does not support multi-line strings. If you want your string to span multiple lines, you have to concatenate multiple strings: String myString = "This is my string" + " which I want to be " + "on multiple lines."; It gets worse though.
There are three ways to create a multiline string in JavaScript. We can use the concatenation operator, a new line character (\n), and template literals. Template literals were introduced in ES6. They also let you add the contents of a variable into a string.
You could use the str.format()
function, that allows named arguments, so:
'''line {0} line {1} line {2}'''.format(1,2,3)
You could of course extend this using Python's *args
syntax to allow you to pass in a tuple
or list
:
args = (1,2,3) '''line {0} line {1} line {2}'''.format(*args)
If you can intelligently name your arguments, the most robust solution (though the most typing-intensive one) would be to use Python's **kwargs
syntax to pass in a dictionary:
args = {'arg1':1, 'arg2':2, 'arg3':3} '''line {arg1} line {arg2} line {arg3}'''.format(**args)
For more information on the str.format()
mini-language, go here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With