Strings: Working with Text

πŸ“Œ Concept 5 of 13✍️ Written by Mark SullivanπŸ“… Reviewed 2026-04-12⏱ ~9 min read

A string is just text β€” but it's text with superpowers. Splitting, searching, replacing, formatting: strings are how programs read and write everything humans care about.

Bee, our debugging mascot

From Bee: Strings are immutable. .toUpperCase() does not change the original β€” it returns a new string. Forgetting this is a top-5 beginner bug.

What Is a String?

A string is a sequence of characters β€” letters, digits, punctuation, spaces, emojis. Quoting tells the language "this is text, not a number or a name." Strings are immutable in most languages: operations like uppercasing return a new string rather than changing the original.

Common Operations

Three categories of string operations show up everywhere:

  • Inspection β€” length, contains, starts/ends with.
  • Transformation β€” uppercase, lowercase, trim, replace, split.
  • Building β€” concatenation (+) or template strings.

Template Strings (Interpolation)

Most modern languages let you embed values directly inside strings instead of concatenating with +:

Code Examples in Three Languages

let name = "Aisha";
let greeting = `Hello, ${name}!`;   // template string

// Common operations
name.length;             // 5
name.toUpperCase();      // "AISHA"
name.includes("isha");   // true
name.replace("A", "Q");  // "Qisha"
"  hi ".trim();          // "hi"
name = "Aisha"
greeting = f"Hello, {name}!"   # f-string

len(name)                 # 5
name.upper()              # "AISHA"
"isha" in name            # True
name.replace("A", "Q")    # "Qisha"
"  hi ".strip()           # "hi"
String name = "Aisha";
String greeting = String.format("Hello, %s!", name);

name.length();            // 5
name.toUpperCase();       // "AISHA"
name.contains("isha");    // true
name.replace("A", "Q");   // "Qisha"
"  hi ".trim();           // "hi"

Best Practices

  1. Use template strings for readability over + concatenation.
  2. Trim user input before validating it.
  3. Compare with === in JS to avoid type coercion.
  4. Be aware of case-sensitivity in comparisons.

Common Mistakes

  • Mutating doesn't work. Strings are immutable β€” assigning s[0] = "X" fails or silently doesn't.
  • Counting bytes vs. characters. Emoji and accented chars can break naive length math.
  • Forgetting to escape quotes. "He said \"hi\"" needs the backslashes.
  • Using + in a loop. In some languages this is slow; use a string builder or join.
πŸ›
See the bugs in action

We have a dedicated Common Bugs with Strings page β€” five real broken snippets with the fix. Read it before you start writing.

How It Works Under the Hood

Strings look like simple text but the storage is more interesting. Most languages encode strings as UTF-16 internally (Java, JavaScript) or UTF-8 (Go, Rust, Python 3). The encoding decides how many bytes a character takes β€” and whether "πŸ˜€".length is 1, 2, or 4.

Because strings are immutable, every "modification" creates a new string. s.toUpperCase() doesn't change s β€” it returns a new string with the upper-cased content. This is why naively concatenating in a loop can be slow: each += creates a fresh string and copies the old one.

The fix is a builder pattern: StringBuilder in Java, "".join(parts) in Python, an array + .join() in JS. Each pre-allocates space and avoids the copy-per-step penalty.

From Beginner to Pro

The same concept gets deeper as you grow. Here's what mastery looks like at three levels:

Beginner

Concatenation with +, length with .length, conversion with .toUpperCase().

Intermediate

You use template literals (JS) and f-strings (Python) for readable interpolation. You know .split(), .join(), .replace(), .includes() by reflex.

Pro

You think about Unicode ("Γ©" as one char or two codepoints?), normalize with String.prototype.normalize, and use a builder when concatenating in a loop. You know regex flavors differ across languages.

Performance & Gotchas

  • Loop concatenation is O(nΒ²) in some languages. Use a builder.
  • Emoji / accents have variable byte width. Naive substring math can split a character.
  • Java == compares references on strings. Use .equals() for content comparison.
  • Locale-aware sorting requires localeCompare or a Collator.

Quick Quiz

Real-World Uses (Production Code, Today)

Search bars

Every search box on the internet runs a string operation β€” substring match, fuzzy match, regex. The same .includes() / .find() functions you learned today.

URLs

A URL is a string with structure. Parsing one means split on / and ?, then split on = β€” pure string work.

Templating

HTML, email, SMS β€” anything with placeholders ({{name}}) is template-string interpolation underneath.

Frequently Asked Questions

How do I check if a string contains a substring?

.includes() in JS, in in Python, .contains() in Java.

What's an f-string?

Python's syntax for embedding expressions inside strings: f"Hello, {name}".

How do I split a string into a list?

.split(",") in all three β€” pass the separator.

Are strings mutable?

In most languages, no β€” operations return new strings rather than modifying the original.

How do I compare strings?

=== in JS, == in Python, .equals() in Java.

What's an "escape character"?

A backslash that signals "the next character is special": \n = newline, \" = literal quote.

How long can a string be?

Practically: gigabytes. Limited mostly by available memory.

Are emojis valid in strings?

Yes β€” they're just Unicode characters. Some operations may surprise you because emojis can take multiple "code units."

βœ…
Key takeaways
  • Strings: Working with Text is one of the 13 universal concepts of programming.
  • The syntax differs across languages, but the underlying idea is the same.
  • Practice in the playground to make it stick.
Was this helpful?
M
Mark Sullivan
Lead writer Β· 8 yrs full-stack

Mark started coding in 2017 after switching from financial analysis. She's built production systems in Python (Django) and JavaScript (Node + React) at two startups, and has taught intro programming at his local community college since 2022. He owns the curriculum for variables, functions, conditionals, and loops on this site. More about Mark β†’