Skip to main content

String

In Turkish
Dize
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/string

In short

A string is a data type that represents text as an ordered sequence of characters, such as a name, a sentence, a URL, or the contents of a file.

What is a string in programming?

A string is a piece of text stored as a sequence of characters, such as letters, digits, spaces, punctuation, and emoji. In most languages you create one by wrapping text in quotes, like "hello" or 'hello', and each character has a position, called an index, that usually starts at 0. Strings are among the most common data types, used for user input, messages, file paths, URLs, and data formats like JSON.

Under the hood, each character is stored as a number according to a character encoding. Modern software uses Unicode, which assigns a number called a code point to characters from almost every writing system, and encodings such as UTF-8 and UTF-16 turn those numbers into bytes. This is why string length can be surprising: in JavaScript, most emoji have a length of 2, because each one takes two UTF-16 code units.

You can picture a string as beads on a necklace, where each bead is one character and the order matters. Languages provide many built-in string operations: joining strings (concatenation), extracting part of one (a substring), searching, replacing, splitting, and changing case. Many also support string interpolation, which inserts values into text, like template literals in JavaScript or f-strings in Python.

In many languages, including JavaScript, Python, Java, and C#, strings are immutable, meaning methods like toUpperCase() return a new string instead of changing the original. A common confusion is between a string and a character: Java, C, and C# have a separate char type for a single character, while JavaScript and Python simply use a string of length one. Another is between the string '42' and the number 42, which look alike but behave differently, so '42' + 1 gives '421' in JavaScript.

Key takeaways

  • A string is an ordered sequence of characters used to represent text.
  • String literals are written in quotes, and characters are accessed by an index starting at 0.
  • Characters are stored as numbers using Unicode encodings such as UTF-8 and UTF-16.
  • In most popular languages strings are immutable, so operations return new strings.
  • The string '42' and the number 42 are different types.

Example

Common string operations in JavaScriptjavascript
const first = "Ada";
const last = "Lovelace";

// Concatenation and a template literal (string interpolation)
const full = first + " " + last;
const greeting = `Hello, ${first}!`;

console.log(full.length);        // 12
console.log(full[0]);            // "A" (indexes start at 0)
console.log(full.toUpperCase()); // "ADA LOVELACE" (a new string)
console.log(full.split(" "));    // ["Ada", "Lovelace"]
console.log("42" + 1);           // "421", not 43

Readers ask

Why are strings immutable in many languages?

Immutable strings are safe to share between parts of a program and between threads, because no code can change them unexpectedly. They also let languages reuse identical strings in memory and use them reliably as keys in hash tables.

What is the difference between a string and a char?

A char holds exactly one character, while a string holds a sequence of zero or more characters. Java, C, and C# have a separate char type, but JavaScript and Python represent a single character as a string of length one.

What is an empty string?

An empty string, written "", is a string with zero characters. It is a real string value, which is different from null or undefined, which mean there is no value at all.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings