How to print or output a String?
Last Updated :
22 Aug, 2023
In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters.
Topics:
|
How to Print or Output a String in C
| printf()
|
How to Print or Output a String in C++
| cout<<
|
How to Print or Output a String in Java
| System.out.println() or System.out.print()
|
How to Print or Output a String in C#
| Console.Write() or Console.WriteLine()
|
How to Print or Output a String in Python
| print()
|
How to Print or Output a String in JavaScript
| console. log()
|
How to Print or Output a String in C?
We can use the printf() function to print a string in C language. It takes the string as an argument in double quotes (" ").
C
#include <stdio.h>
// Drivers code
int main()
{
char str[13] = "GeeksforGeeks";
printf("String: ");
// %s is used to print a string
printf("\"%s\"", str);
return 0;
}
OutputString: "GeeksforGeeks"
How to Print or Output a String in C++?
To print the string, just place the string (the variable that stores the string's value) after cout<<, as shown here in the following program.
C++
#include <iostream>
using namespace std;
int main()
{
string str = "GeeksforGeeks";
cout << "String: ";
// Printing string using cout
cout << str;
return 0;
}
OutputString: GeeksforGeeks
How to Print or Output a String in Java?
In Java, we can simply use System.out.println() or System.out.print() methods to print the string, where,
- System is a class
- out is a public static field: it accepts output data.
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
String str = "GeeksforGeeks";
// Printing on same line using print()
System.out.print("String: ");
System.out.print(str);
// Printing on next line using println()
System.out.println("\nString: ");
System.out.println(str);
}
}
OutputString: GeeksforGeeks
String:
GeeksforGeeks
How to Print or Output a String in C#?
A string is represented by class System.String. The “string” keyword is an alias for System.String class and instead of writing System.String one can use String which is a shorthand for System.String class.
Now to print the String in C#, we need to use the Console.Write() or Console.WriteLine() method, as shown below:
C#
using System;
public class GFG {
static public void Main()
{
String str = "GeeksforGeeks";
Console.Write("String: {0}", str);
}
}
OutputString: GeeksforGeeks
How to Print or Output a String in Python?
Python print() function prints the message to the screen or any other standard output device.
Example
In this example, we have created three variables integer, string and float and we are printing all the variables with print() function in Python.
Python3
name = "GeeksforGeeks"
# Printing string using print() method
print("String: ", name)
OutputString: GeeksforGeeks
How to Print or Output a String in JavaScript?
In JavaScript, you can print a string using "console. log()" or document.write() method.
JavaScript
let str = "GeeksforGeeks";
// Printing the string
console.log(str);
Similar Reads
How to Print a String in JavaScript ? In JavaScript, printing a string means displaying the contents of a string variable or literal to the console, webpage, or any other output medium. Strings are a fundamental data type in JavaScript, representing sequences of characters enclosed within single ('') or double ("") quotes. Table of Cont
2 min read
How to print output in Ruby? An essential aspect of programming involves the ability to print output, This allows developers to not only communicate information with users but also debug code and present results. Within Ruby, a potent and adaptable language for programming, numerous methods exist for printing output directly in
4 min read
How to read or input a string? In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. Topics: How
3 min read
Printing Output of an R Program In R there are various methods to print the output. Most common method to print output in R program, there is a function called print() is used. Also if the program of R is written over the console line by line then the output is printed normally, no need to use any function for print that output. T
4 min read
Printing Output on Screen in Julia Julia provides many methods of printing output on the screen. The Julia program starts with an interactive REPL (Read/ Evaluate /Print / Loop) as default. R: Reads what was typed;E: Evaluates the typed expression;P: Prints the return value;L: Loops back and repeats it ; It helps in outputting the r
3 min read
How to Print String and Variable on Same Line in R Printing a string and a variable on the same line is useful for improving readability, concatenating dynamic output, aiding in debugging by displaying variable values, and formatting output for reports or user display. Below are different approaches to printing String and Variable on the Same Line u
3 min read
How to Print String and Int in the Same Line in Python Printing both texts and integers on the same line is a common chore for programmers. Python, fortunately, has various methods for accomplishing this, making it simple to display a combination of text and numerical values in your output. In this article, we'll explore various ways to print strings an
2 min read
How to Print String Literal and Qstring With Qdebug in C++? Printing string literals and QString with QDebug in C++ can be a useful tool for debugging. By printing out the contents of a string or QString, you can quickly identify any issues that may be present in your code. In this article, weâll discuss how to print string literals and QString with QDebug i
2 min read
How to print on browser's console using PHP ? The echo command is used in PHP to print any value to the HTML document. Use <script> tag inside echo command to print to the console. Syntax: echo "This is GeeksforGeeks Tutorial."; echo variable_name; Note: The echo command will print the HTML document. This feature can be used to put JavaSc
2 min read
C++ Program to Print Your Own Name Printing your own name means displaying your name on the computer screen. In this article, we will learn how to print your own name using a C++ program.ExamplesInput: name = "Anmol"Output: AnmolExplanation: Given name is printed on the output screen.Input: name = "Alex"Output: AlexExplanation: Given
3 min read