Introduction to Strings
Table of Contents + β
You already use strings every single day. Your name. A WhatsApp message. A YouTube search. A password. All of these are just text. In programming we call this text a string. So before you can build anything real, you need to understand how the computer holds your words. That is what this lesson is about.
π What Is a String?
A string is a sequence of characters placed one after another, like a word or a sentence.
A character is one single symbol. The letter A is a character. The digit 7 is a character. Even a space is a character. When you line up many characters in order, you get a string.
So "Riya" is a string made of four characters: R, i, y, a.
Here is an easy way to picture it. Think of a string like beads on a thread. Each bead is one character. The thread keeps them in order. Pull the thread and the word stays as Riya, not yaRi. So order matters in a string.
Note
We usually write a string inside quotes, like "hello". The quotes are not part of the string. They just tell the computer where the text starts and where it ends.
π§ How a String Is Stored Inside the Computer
Now the interesting part. The computer does not actually store letters. It only stores numbers. So how does it keep your name?
The trick is simple. Every character is given a number. The computer agreed on a shared table that maps each character to a number. This table is called ASCII, which stands for American Standard Code for Information Interchange. It is just a lookup list. For example A is 65, B is 66, and a small a is 97. Even a space has a number, and that number is 32.
So when you store "Hi", the computer is really storing the numbers 72 and 105, sitting next to each other in memory.
Let us see the word Hi the way the computer sees it.
A string is laid out in memory just like a character array. The characters sit in neighboring boxes, in order, and each box holds one number.
Tip
ASCII only covers English letters and basic symbols. To store emojis, Hindi, Tamil, Chinese and almost every language, computers use a bigger table called Unicode. Unicode works the same way. It is still characters mapped to numbers, just a much larger list.
Let us actually print the number behind a character so you believe it. The code below takes the letter A and shows the number the computer stores for it.
# Print the ASCII number behind a characterdef show_code(ch): # ord() gives the number for a character print(f"The character '{ch}' is stored as {ord(ch)}")
show_code('A')The output of the above code will be:
The character 'A' is stored as 65See, the same letter A gives 65 in every language. That is because the ASCII table is shared by all of them.
βοΈ Creating and Printing a String
Now let us make a real string, print it, and also find how long it is.
The length of a string is just the count of characters in it. The word Hello has 5 characters, so its length is 5.
Here is the same task in all five languages. We create the string Hello, print it, then print its length.
# Create a stringgreeting = "Hello"
print("The string is:", greeting)# len() counts the charactersprint("Its length is:", len(greeting))The output of the above code will be:
The string is: HelloIts length is: 5The words for βcreateβ and βlengthβ change a little in each language. But the idea is exactly the same everywhere. You give the text a name. You print it. Then you ask how many characters it holds.
π’ Picking One Character: Indexing
Often you do not want the whole string. You want just one character from it. Maybe the first letter of a name. Maybe the last digit of a code. For that we use the index.
The index is the position number of a character inside the string. And here is the part that surprises every beginner. Counting starts at 0, not 1.
So in Hello the positions look like this.
| Character | H | e | l | l | o |
|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 |
So index 0 is H, the very first character. Index 4 is o, the last one. To reach a character you write the string name and then the index in square brackets, like greeting[0].
The code below grabs the character at index 0 from Hello.
greeting = "Hello"
# Square brackets reach one character by its indexprint("First character:", greeting[0])The output of the above code will be:
First character: HCaution
A very common beginner mistake is asking for greeting[5] in Hello. The last valid index is 4, because counting started at 0. So index 5 is past the end. In some languages this crashes the program. So always remember that the last index is the length minus one.
β οΈ Common Mistakes Beginners Make
A few small things trip up almost everyone at the start. Keep these in mind.
- Forgetting that the index starts at 0. The first character is at index 0, never index 1.
- Counting the quotes as part of the string.
"Hi"has 2 characters, not 4. The quotes just mark the start and the end. - Thinking a space is empty. A space is a real character with its own number, and it adds to the length.
- Mixing up a character and a string.
'A'is one character."A"is a string that happens to hold one character. Some languages care about this difference a lot.
π§© What Youβve Learned
β A string is a sequence of characters in order, like a word or a sentence.
β Inside the computer a string is stored like a character array, with characters sitting next to each other.
β Every character is really a number, mapped by the ASCII table, and Unicode extends this to every language and emoji.
β You can create a string, print it, and find its length in every language with the same idea.
β Indexing reaches one character by its position, and counting always starts at 0.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is a string?
Why: A string is just characters lined up in order, like a word or a sentence.
- 2
Inside the computer, how is each character actually stored?
Why: Computers store numbers, so each character is mapped to a number through tables like ASCII or Unicode.
- 3
In the string "Hello", what index holds the first character 'H'?
Why: Counting starts at 0, so the first character sits at index 0.
- 4
What is the length of the string "Hi"?
Why: Length is the count of characters, and the quotes are not part of the string, so "Hi" has length 2.