Open In App

How to Declare & Initialise a String in different Programming languages?

Last Updated : 11 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

To declare and initialize a string in different programming languages, you can use language-specific syntax and functions. Below are examples in C++, C#, Python, JavaScript, and Java, with explanations for each language:

How to Declare & Initialise a String in C++

Steps:

  1. Include the necessary header files ( #include<string> ).
  2. Declare a string variable using the std::string type.
  3. Initialize the string variable with the desired value.

For example:

C++
#include <iostream>
#include <string>

int main() {
    // Declare and initialize a string
    std::string myString = "Hello, World!";
    
    // Print the string
    std::cout << myString << std::endl;

    return 0;
}
JavaScript
// Declare and initialize a string
const GFG = "Hello, World!";

// Print the string
console.log(GFG);

Output
Hello, World!

How to Declare & Initialise a String in C#

Steps:

  1. Declare a string variable using the string type.
  2. Initialize the string variable with the desired value.

For example:

C#
using System;

class Program {
    static void Main() {
        // Declare and initialize a string
        string myString = "Hello, World!";
        
        // Print the string
        Console.WriteLine(myString);
    }
}

Output
Hello, World!

How to Declare & Initialise a String in Python

Steps:

  1. Declare a variable and assign it a string using single or double quotes.

For example:

Python3
# Declare and initialize a string
my_string = "Hello, World!"

# Print the string
print(my_string)

Output
Hello, World!

How to Declare & Initialise a String in JavaScript

Steps:

  1. Declare a string variable using the var, let, or const keyword.
  2. Initialize the string variable with the desired value by enclosing the text in single or double quotes.

For example:

JavaScript
// Declare and initialize a string
const myString = "Hello, World!";

// Print the string
console.log(myString);

Output
Hello, World!

How to Declare & Initialise a String in Java

Steps:

  1. Declare a string variable using the String type.
  2. Initialize the string variable with the desired value using double quotes.

For example:

Java
/*package whatever //do not write package name here */

import java.io.*;
public class StringInitializationExample {
    public static void main(String[] args) {
        // Declare and initialize a string
        String myString = "Hello, World!";
        
        // Print the string
        System.out.println(myString);
    }
}

Output
Hello, World!


In each of these programming languages, you can declare and initialize a string by declaring a variable of the appropriate string type and assigning a string literal to it. The specific syntax may vary, but the general idea is the same: you create a string variable and provide the initial string value.


Next Article
Article Tags :
Practice Tags :

Similar Reads