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 (” “).
#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;
} |
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.
#include <iostream> using namespace std;
int main()
{ string str = "GeeksforGeeks" ;
cout << "String: " ;
// Printing string using cout
cout << str;
return 0;
} |
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.
/*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);
}
} |
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:
using System;
public class GFG {
static public void Main()
{
String str = "GeeksforGeeks" ;
Console.Write( "String: {0}" , str);
}
} |
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.
name = "GeeksforGeeks"
# Printing string using print() method print ( "String: " , name)
|
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.
let str = "GeeksforGeeks" ;
// Printing the string console.log(str); |
GeeksforGeeks