Are you learning to code, or reading an error message, and the word string keeps coming up as if everybody already knows what is a string?
In programming, a string is text. Any run of characters a program handles as words rather than numbers is a string: a name, a password, an email address, a sentence, even a single letter. The name comes from the idea of characters strung together, one after another.
Picture a necklace of beads, where every bead is one character, spaces and punctuation included. The program keeps the beads in order and can count them, cut the necklace, or join two together, and it never tries to add them up like numbers.
The things that confuse people about strings are:
- How quotes tell the program where a string starts and ends
- Why “5” in quotes is not the number 5
- What happens when the text itself contains a quote mark
- Why joining strings and adding numbers can give strange results
In this article you will learn all four, with short examples in JavaScript and Python, the two languages beginners meet most.
So let’s get started.
Quotes mark the start and the end
A string is written between quote marks, so the program knows exactly where the text begins and ends:
name = "Amina"
greeting = 'Hello there'
JavaScript and Python accept double or single quotes, as long as both ends match. Leave one out and the program cannot tell where the text stops, which is one of the most common causes of a syntax error.
“5” is not 5
“5” in quotes is a string holding the character five. 5 without quotes is a number. They look the same on screen and behave completely differently.
In JavaScript, “5” + 3 gives “53”, because the + joins text when either side is a string. 5 + 3 gives 8. In Python, “5” + 3 is an error, because Python refuses to guess which one you meant.
This is the source of the classic form bug where a total comes out as 1020 instead of 30. Anything typed into a web form arrives as a string, and it has to be turned into a number before any sums are done.
Quotes inside the text
If the text itself contains a quote mark, wrap it in the other kind of quotes, or put a backslash before the inner one:
message = "It's raining"
quote = 'She said "yes"'
escaped = "She said \"yes\""
The backslash tells the program the next quote is part of the text, not the end of it. The same idea shows up in JSON, where only double quotes are allowed, and our guide on creating a JSON file shows why that trips people up.
The things you can do with a string
Every language lets you find its length, change it to capitals or small letters, cut a piece out, search inside it, replace part of it, and split it into a list at every comma or space.
One detail catches everybody once: counting starts at zero. The first character of “Hello” is at position 0 and the last is at position 4, which is why length and last position always differ by one.
Keep in mind too that strings usually cannot be changed in place. Making a capitalised version gives you a new string, and the original stays as it was until you store the new one.
Strings and numbers that look alike
Phone numbers, postcodes and account numbers are best kept as strings even though they are made of digits. You never add two phone numbers together, and storing one as a number loses the zero at the front.
If you are new to programming, I recommend asking of any value whether you will ever do sums with it. If not, it is a string, whatever it looks like.
Where to go next
JavaScript is the language most people start with, and our guide to what JavaScript is explains where it runs. For Python, the invalid syntax error page covers the unclosed quotes and curly quotes that break strings most often.
FAQ(What Is a String)
What is a string in simple words?
Text that a program stores and works with, such as a name, a sentence or a password.
Why is it called a string?
Because it is a sequence of characters strung together one after another, like beads on a thread.
Is a single letter a string?
In JavaScript and Python, yes. Some languages, such as Java and C, also have a separate type for exactly one character.
Is a number in quotes a string?
Yes. “42” is text made of two characters, and it has to be converted before it can be used in sums.
What is an empty string?
A string with nothing in it, written as two quote marks with nothing between. It exists and has a length of zero.
If you have any issues, you can ask me via comment, and I will love to help you out.