Printing multiple variables inside quotes in python
You should enclose the entire string with single quotes ('
) and each %s
with double quotes ("
):
print 'Hostname="%s" IP="%s" tags="%s"' % (name, value["ip"], tags)
Printing multiple variables in a separate lines using a single print
In python3:
print(string1, string2, sep='\n')
In python2:
print string1 + '\n' + string2
... or from __future__ import print_function
and use python3's print
Since my first answer, OP has edited the question with a variable type change. Updating answer for the updated question:
If you have some integers, namely int1
and int2
:
Python 3:
print(int1, int2, sep='\n')
Python 2:
print str(int1) + '\n' + str(int2)
or
from __future__ import print_function
print(int1, int2, sep='\n')
or
print '\n'.join([str(i) for i in [int1, int2]])
print multiple variables in a string what is the correct syntax
The second argument would be arg1 "arg2"
which is not a valid expression. If you want to print both arg1
and "arg2"
, you should separate it by another comma:
print("arg1 =", arg1, "arg2 =", arg2)
Python: Accept multiple arguments
*args and **kwargs allow to pass any no of arguments, positional (*) and keyword (**) to the function
>>> def test(*args):
... for var in args:
... print var
...
>>>
For no of variable
>>> test("a",1,"b",2)
a
1
b
2
>>> test(1,2,3)
1
2
3
For list & dict
>>> a = [1,2,3,4,5,6]
>>> test(a)
[1, 2, 3, 4, 5, 6]
>>> b = {'a':1,'b':2,'c':3}
>>> test(b)
{'a': 1, 'c': 3, 'b': 2}
For detail
Print more than one value
Just use commas to seperate arguments:
print("Hello ", name, ", how are you?", sep='')
You can also use the f string formatter:
print(f"Hello {name}, how are you?")
or also with str.format()
:
print("Hello {}, how are you?".format(name))
Is there a way I can print multiple lines using one print()?
If you'd like to have a preconfigured multiline block of text to print, and just add some values to it (bit like doing a mail-merge in Word), you can use the str.format
method.
>>> help(str.format)
format(...)
| S.format(*args, **kwargs) -> str
|
| Return a formatted version of S, using substitutions from args and kwargs.
| The substitutions are identified by braces ('{' and '}').
Multiline strings have """
(or, less commonly, '''
).
template = """{name} is a {role}.
Age: {age}
Height: {height} metres
Weight: {weight} milligrams"""
gabh = template.format(
name="Gabh",
role="Musician",
age=21,
height=5.4,
weight=47
)
print(gabh)
(This is slightly different to f-strings, where values get put into the string at the moment it's created.)
If you have a dictionary with keys matching the {stuff} in {curly braces}
in your template string, you can use format_map
:
template = """{name} is a {role}.
Age: {age}
Height: {height} metres
Weight: {weight} milligrams"""
gabh = {
"name": "Gabh",
"role": "Musician",
"age": 21,
"height": 5.4,
"weight": 47,
}
print(template.format_map(gabh))
variable positional and printing the values
The first output shows a comma because without it, 1
being the only element, (1)
would be just a integer (parentheses are wrapping the expression 1
), (1,)
is shown to differentiate tuples and simple parentheses.
in the second one, no trailing comma is needed to differentiate tuples, since there are more than one element.
In the third O/p, you are not passing 1
and 2
, but instead you're passing the whole (1,2)
, so it shows only one item (which is (1,2)
) in a tuple, and adds an extra comma. Same for the fourth: your passing the entire ((1,2), (3,4))
.
Related Topics
What Is the Purpose of the -M Switch
How to Check If a String Contains an Element from a List in Python
Creating Dataframe from a Dictionary Where Entries Have Different Lengths
Adding Days to a Date in Python
String Concatenation of Two Pandas Columns
How to Get Monitor Resolution in Python
How to Convert Each Item in the List to String, for the Purpose of Joining Them
Non-Alphanumeric List Order from Os.Listdir()
Importerror: Dll Load Failed: %1 Is Not a Valid Win32 Application. But the Dll's Are There
Pandas: Drop Consecutive Duplicates
Why Python 3.6.1 Throws Attributeerror: Module 'Enum' Has No Attribute 'Intflag'
Creating a Simple Xml File Using Python
How to Get the Named Parameters from a Url Using Flask
Set Matplotlib Colorbar Size to Match Graph
Attributeerror: 'Module' Object Has No Attribute