Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Strings · Lesson 23 of 25

std::string

A free lesson from Swipe Coding · C++, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Text needs its own type — here's C++'s answer. 🧵

std::string is the type C++ uses for text made of more than one character. Unlike the primitives you just learned, std::string is not a primitive type — it comes from the standard library, so you need #include <string> at the top of your file to use it.

#include <iostream>
#include <string>

int main() {
    std::string name = "Ada";
    int points = 95;
    std::cout << "Hello, " + name + "!" << std::endl;
    std::cout << "Score: " << points << std::endl;
    return 0;
}
The + operator concatenates (joins) strings together. It works between two std::string values, or between a std::string and plain quoted text.
🤖
These come up constantly once you start working with real text. 🔧

std::string comes with many useful member functions. .length() (or its twin, .size()) returns how many characters are in the string. You can also grab a single character by its position — counting from 0 — using square brackets.

std::string word = "Code";
std::cout << word.length() << std::endl; // 4
std::cout << word[0] << std::endl;        // C
.length() and square-bracket indexing are some of the most frequently used std::string operations.
Fill in the blank

To find out how many characters are in a std::string called word, you write ____.

🤖
Here's a genuine difference from some other languages. 🔓

Unlike text types in some other languages, std::string is mutable: you can change individual characters after the string is created, using square brackets on the left side of an assignment. You can also compare two strings directly with == — it compares their actual text content, not where they live in memory.

std::string word = "Cat";
word[0] = 'B';
std::cout << word << std::endl; // Bat

std::string a = "cat";
std::string b = "cat";
std::cout << (a == b) << std::endl; // 1 (true)
Changing word[0] mutates the string in place, and == compares the two strings' text content directly.
True or false

You can compare the text content of two std::string values directly with the == operator.

Quick check

What does word[0] = 'B'; do if word currently holds "Cat"?

🤖
Small distinction, but worth remembering. 📌

One last thing to keep straight: a std::string is written with double quotes ("Ada"), while a single char is written with single quotes ('A'). Mixing them up is a very common early mistake — the compiler will complain if you try to put more than one character between single quotes.

Chapter quiz

Strings

10 questions · pass with 60% — just like in the app.

Quick check

Which header must you #include before using std::string?

True or false

std::string is one of C++'s primitive types, just like int and bool.

Fill in the blank

To find out how many characters a std::string called word contains, you write ____.

Quick check

std::string word = "Code"; What does word[0] give you?

True or false

std::string is mutable — you can change an individual character after the string was created.

Fill in the blank

The ____ operator joins (concatenates) two std::string values into one.

Quick check

std::string word = "Cat"; word[0] = 'B'; What does word hold afterward?

True or false

Two separate std::string variables holding identical text compare equal with ==.

Quick check

What is the difference between "A" and 'A' in C++?

Fill in the blank

A single char is written with ____ quotes, while a std::string is written with double quotes.

Keep your streak going in Swipe Coding

Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →