Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print values without new line

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?

like image 391
Codoscope Avatar asked Aug 31 '17 15:08

Codoscope


People also ask

How do you print without newline in Python?

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.

How do you print a variable without spaces between values?

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 '' .

How will you print space and stay on the same line in Python?

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.

How do you print only one line in Python?

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.


1 Answers

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.

like image 95
Matt P Avatar answered Sep 20 '22 10:09

Matt P