Language: EN


Type of text

Text types are a category of variables in programming that allow us to manipulate and work with text data, such as words, phrases, names, and messages.

These types of variables are very common in our program. For example, a person’s name, a shipping address, or the text to display to the user in a dialog window.

This is because humans use words and text to communicate. However, for a computer, handling text is not as simple and natural. So we have had to build the necessary structures to manage them.

In general, we will frequently find two types of text variables in different programming languages

  • Char, for individual characters
  • String, for complete text strings

The char type

The char variable type, short for “character” in English, is the basic unit of text used in many programming languages. It represents a single character, such as a letter, a number, or a symbol.

In reality, a computer understands nothing about characters or text. They only know how to handle numbers. So how are characters stored in a computer?

This is where the character translation table comes into play, such as ASCII or Unicode. These tables assign a unique number to each character. For example, the character “A” is represented by the number 65 in the ASCII table.

In this way, when we work with char variables in a program, we are actually manipulating the numbers that represent the characters.

The string type

While char is the basic unit of text, in most cases we need to work with longer text strings that contain multiple characters. This is where the string variable type comes into play.

A string is the “real text” variable type. Unlike char, which represents a single character, a String can contain a word, a phrase, or even a complete paragraph.

In many programming languages, a string is simply a collection of char. For example, in C.

However, to facilitate the handling and manipulation of text strings, programming languages generally provide more complex objects, specifically designed to work with text.

Example of text variable types in different languages

In languages derived from C, such as C++, C#, or Java, both types, char and string, exist. For example, here’s how you would create a variable of each type in C#

char myCharacter = 'A';
string myString = "Hello, world!";

Which is identical to the case of C++, with the exception that string is defined in the std namespace.

char myCharacter = 'A';
std::string myString = "Hello, world!";

Notice that in these languages, the value of a char is represented by ', while for string values, " is used.

In JavaScript, only the string type exists, with no specific char type. Also, for the use of values, we can use ', ", or ` interchangeably.

let myCharacter = 'A';
let myString = "Hello, world!";

Similarly in Python, only the string type exists. To indicate values, we can use either ' or " interchangeably.

myCharacter = 'A'
myString = "Hello, world!"