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
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;
}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
To find out how many characters are in a std::string called word, you write ____.
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)
You can compare the text content of two std::string values directly with the == operator.
What does word[0] = 'B'; do if word currently holds "Cat"?
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.
Strings
10 questions · pass with 60% — just like in the app.
Which header must you #include before using std::string?
std::string is one of C++'s primitive types, just like int and bool.
To find out how many characters a std::string called word contains, you write ____.
std::string word = "Code"; What does word[0] give you?
std::string is mutable — you can change an individual character after the string was created.
The ____ operator joins (concatenates) two std::string values into one.
std::string word = "Cat"; word[0] = 'B'; What does word hold afterward?
Two separate std::string variables holding identical text compare equal with ==.
What is the difference between "A" and 'A' in C++?
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 →