How to print many values without line breaks?
PRINT *, "foo:", foo, ", bar:", bar, ", baz:", baz
Apparently, this is possible with WRITE
(here and there). How to achieve the same with PRINT
and its different syntax while printing several values?
It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.
To print multiple values or variables without the default single space character in between, use the print() function with the optional separator keyword argument sep and set it to the empty string '' .
The end=” “ is used to print in the same line with space after each element. It prints a space after each element in the same line.
Use print(i,end="\r") to return to the start of the line and overwrite. Save this answer. Show activity on this post. this is easiest way to print in one line.
The write
statement provides an optional advance
specifier, but print
does not.
Multiple write
statements with advance="no"
can be made at different places in your code in order to print multiple items to the same line. Just as an example, using it from within a do
loop:
do i=1,3
write(*, fmt="(1x,a,i0)", advance="no") "loop #", i
end do
write(*,*) ! Assumes default "advance='yes'".
write(*,*) "--OK, the loop is done!"
! Example output:
loop #1 loop #2 loop #3
--OK, the loop is done!
Note that advance
can't be used with list-directed output (using the "*" to "print anything"). Therefore, I've shown an example format specifier fmt="(1x,a,i0)"
which will print a single blank space, a character string, and a single integer for each write statement. A language reference and/or your compiler documentation comes in handy. See, here, for example.
As others have suggested, if this is the desired behavior, it's best to use write
. If for some reason you still prefer to use print
, then you should probably assemble your output items into a single variable or list of variables before printing it.
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