Facebook Pixel

What is a String?

A player enters their username into a game: "DragonSlayer99". The chat system needs to store this text, display it above the character, and check if it contains inappropriate words. Arrays store numbers at indexed positions, but how do programs store and work with text?

A string is a sequence of characters. Just like an array stores multiple numbers at indexed positions, a string stores multiple characters at indexed positions. The username "DragonSlayer99" contains 14 characters, numbered from position 0 to position 13.

String Structure

Each character occupies one position in the string. The character 'D' sits at position 0, 'r' at position 1, 'a' at position 2, and so on. The string knows its total length (14 characters) and can access any character by its position number.

Strings are Arrays of Characters

Under the surface, a string behaves like an array where each element is a single character. When you create a string "Hello", you're creating a structure that stores five characters in sequence: 'H' at index 0, 'e' at index 1, 'l' at index 2, 'l' at index 3, and 'o' at index 4.

String as Array

Different programming languages handle the character array structure differently, but they all provide the same basic operations: access characters by position, find the length, and iterate through each character.

1text = "Hello"
2print(len(text))     # 5
3print(text[0])       # 'H'
4print(text[4])       # 'o'

Python strings are immutable - you cannot change individual characters after creation.

Memory Storage

Strings live in the heap, just like arrays. When you create a string variable, that variable holds a reference pointing to the actual character data stored in heap memory. Multiple variables can reference the same string data in memory.

String Memory Storage

When you write name = "Alice", the program allocates space in the heap for the characters 'A', 'l', 'i', 'c', 'e'. The variable name stores a reference to that memory location. If you assign other_name = name, both variables now reference the same string data in memory.

Strings serve as the foundation for processing text in programs. Chat messages, user input, file contents, web pages, and API responses all arrive as strings. Understanding how strings store characters in sequence lets you manipulate text: search for words, validate formats, parse commands, and transform content.

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro