How to print a line?

How to print a string as a line, That is, the string and the newline character, nicely?

asked Apr 6 by anonymous

8 Answers

In Bash, you can simply use the echo command:

echo "your message here"


echo your message here


$ echo the message here
the message here
$ echo "the message here"
the message here
$ echo `date`
Fri Apr 7 13:40:20 HKT 2017
answered Apr 6 by zma (2,200 points)

In Python, to print a string str with a new line:

print str
answered Apr 7 by zma (2,200 points)

In Go, you can use the 'Println()' func from the 'fmt' package:


fmt.Println("my msg here")
answered Apr 7 by zma (2,200 points)

In Java, you can print string in a new line by 'System.out.println()':

System.out.println("my msg here");
answered Apr 7 by zma (2,200 points)
edited Apr 11 by zma

In C, you may do this:

printf("%s\n", your_str);

For example,

$ cat t.c
#include <stdio.h>

void main()
  printf("%s\n", "hello world!");

$ gcc t.c -o t && ./t
hello world!
answered Apr 8 by zma (2,200 points)

In PHP, you may print a line using echo by appending the PHP_EOL to the string:

echo $your_msg . PHP_EOL;

For example,

$ php -a
Interactive shell

php > echo "hello world!" . PHP_EOL;
hello world!
php > 
answered Apr 8 by zma (2,200 points)

In C++, you may print the string and then '\n' or std::endl:

std::cout << your_string << std::endl;


std::cout << your_string << '\n';


$ cat a.cpp
#include <iostream>

int main()
  std::cout << "hello world!" << std::endl;
  std::cout << "happy Dtivl!" << '\n';
  return 0;

$ g++ a.cpp -o a && ./a
hello world!
happy Dtivl!
answered Apr 8 by zma (2,200 points)

In OCaml, you may print a string with the end line character by using the function in the Pervasives module:

val print_endline : string -> unit

Print a string, followed by a newline character, on standard output and flush standard output.

One usage example:

$ ocaml
        OCaml version 4.01.1+dev2-2013-12-18+CLOSED

# print_endline "hello world!";;
hello world!
- : unit = ()
answered Apr 18 by zma (2,200 points)

Please log in or register to answer this question.

Related questions

0 answers
7 answers
asked Mar 31 by zma (2,200 points)
8 answers
asked Apr 1 by zma (2,200 points)
0 answers
Welcome to Do This In Various Langs (dtivl), where you can ask questions and receive solutions in various programming languages.
Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost