Open In App

How to print or output a String?

Last Updated : 22 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Language Used

Function or Classes

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;
}


Output

String: "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;
}


Output

String: 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);
    }
}


Output

String: 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);
    }
}


Output

String: 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)


Output

String:  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);


Output

GeeksforGeeks


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads