can't have new line in python with two variable using n
Question:
why i can’t have a new line with two variables while i can with a variable and string?
I try using the n but it does not works
Thank you.
a = 10
b = 2
print(int(a//b)+n+ float(a//b))
need output to be;
5
5.00
Answers:
You have to do something like this to mix different object-types:
print(str(int(a//b))+'n'+ str(float(a//b)))
I would use an f-string
to make your life easier:
a = 10
b = 2
print(f'{int(a//b)}n{float(a//b)}')
Output:
5
5.0
Note if you want more decimal places in the float value you can use a format specifier:
print(f'{int(a//b)}n{float(a//b):.2f}')
Output:
5
5.00
Note (as pointed out by @ShadowRanger in their answer), //
returns an integer when using integer inputs so there is no need to convert it to one using int
.
I’d personally go with Nick’s answer (it’s the most flexible, and it reads nicely; among other things, it makes it trivial to enforce the two digit precision you’re expecting, where plain str()
conversions will never turn float(5)
into 5.00
), but for simple cases like "Must print a few distinct values on separate lines", you can just change the default separator from a single space to a newline, and print the values as separate arguments:
print(a//b, float(a//b), sep='n')
which outputs:
5
5.0
Note that I’m not trying to concatenate strings here (no +
in sight), just specifying the two things to print as separate positional arguments, and telling print
what to put between them.
Side-note: I removed the int()
call, because when the inputs are both int
, and you use //
, the floor division operator, the result is already an int
, so calling it is redundant. I also strongly suspect floor(a//b)
is not what you want (since it converts to float
after floor division; if the result wasn’t already evenly divisible, you’d lose information to the floor division, and the "cast" to float
couldn’t recover it). Most likely, what you really want is:
print(a//b, a/b, sep='n')
using /
in the second case for "true division" (where, when the inputs are int
s or float
s, the result is a float
with the result, including as much precision as float
can handle for the result of dividing the two operands). The output doesn’t change for your specific example inputs, but if the inputs were, say, a = 15
and b = 12
, you’d get an output of:
1
1.25
where using your current float(a//b)
would produce:
1
1.0
why i can’t have a new line with two variables while i can with a variable and string?
I try using the n but it does not works
Thank you.
a = 10
b = 2
print(int(a//b)+n+ float(a//b))
need output to be;
5
5.00
You have to do something like this to mix different object-types:
print(str(int(a//b))+'n'+ str(float(a//b)))
I would use an f-string
to make your life easier:
a = 10
b = 2
print(f'{int(a//b)}n{float(a//b)}')
Output:
5
5.0
Note if you want more decimal places in the float value you can use a format specifier:
print(f'{int(a//b)}n{float(a//b):.2f}')
Output:
5
5.00
Note (as pointed out by @ShadowRanger in their answer), //
returns an integer when using integer inputs so there is no need to convert it to one using int
.
I’d personally go with Nick’s answer (it’s the most flexible, and it reads nicely; among other things, it makes it trivial to enforce the two digit precision you’re expecting, where plain str()
conversions will never turn float(5)
into 5.00
), but for simple cases like "Must print a few distinct values on separate lines", you can just change the default separator from a single space to a newline, and print the values as separate arguments:
print(a//b, float(a//b), sep='n')
which outputs:
5
5.0
Note that I’m not trying to concatenate strings here (no +
in sight), just specifying the two things to print as separate positional arguments, and telling print
what to put between them.
Side-note: I removed the int()
call, because when the inputs are both int
, and you use //
, the floor division operator, the result is already an int
, so calling it is redundant. I also strongly suspect floor(a//b)
is not what you want (since it converts to float
after floor division; if the result wasn’t already evenly divisible, you’d lose information to the floor division, and the "cast" to float
couldn’t recover it). Most likely, what you really want is:
print(a//b, a/b, sep='n')
using /
in the second case for "true division" (where, when the inputs are int
s or float
s, the result is a float
with the result, including as much precision as float
can handle for the result of dividing the two operands). The output doesn’t change for your specific example inputs, but if the inputs were, say, a = 15
and b = 12
, you’d get an output of:
1
1.25
where using your current float(a//b)
would produce:
1
1.0