How to print without a newline or space?

 

To print without adding a newline or space in various programming languages, you usually need to use specific functions or methods that allow you to control the formatting. Here are examples in different programming languages:

Python: In Python, you can use the end parameter of the print function to specify what should be added after the print statement. By default, end is set to '\n' (newline). You can change it to an empty string to print without a newline.

python
print("Hello", end='') print("World")

C: In C, you can use the printf function to print without a newline. Just don't include the \n character at the end of your format string.

c
#include <stdio.h> int main() { printf("Hello"); printf("World\n"); return 0; }

Java: In Java, you can use the System.out.print method to print without a newline. The System.out.println method adds a newline, while System.out.print does not.

java
public class Main { public static void main(String[] args) { System.out.print("Hello"); System.out.print("World\n"); } }

C++: In C++, you can use the std::cout stream to print without a newline. Just like in C, not adding endl (which adds a newline) keeps the output on the same line.

cpp
#include <iostream> int main() { std::cout << "Hello"; std::cout << "World" << std::endl; return 0; }

In all of these examples, by not including the newline character or a space, you ensure that the output remains on the same line without any extra spacing or line breaks.

Comments