Introduction to Strings
Explore the concept of strings as ordered sequences of characters and understand their storage, key properties, and significance in algorithmic problems. Learn how Python represents and manipulates strings, the difference between characters and strings, and why strings are essential in real-world text data processing.
By this point, you are familiar with arrays as ordered collections of elements stored by position. Arrays provide a general way to store sequences of values such as integers, floating-point numbers, and objects. Each element in an array can be accessed efficiently using its index.
However, many important computational problems involve textual and symbolic data rather than only numeric values. Names, passwords, file paths, search queries, source code, and biological sequences are all examples of data that must be handled as sequences of characters. Although arrays can store characters, viewing such data only as a general array is often not enough for algorithmic study.
Character-based data gives rise to a different kind of problem. In numerical arrays, common tasks include searching for values, computing sums, or rearranging elements. In character sequences, the focus shifts to questions such as whether two words are equal, whether a pattern appears inside a larger text, how often a character occurs, whether a string is a palindrome, and how two strings compare.
Strings provide a natural way to represent this type of data. A string is an ordered sequence of characters, and it forms the basis of many important computational tasks such as text processing, pattern matching, and validation.
What is a character?
Before defining a string precisely, we need to be clear about what a string is made of. The basic building block of every string is a character.
A character is a single symbol. It can be a letter like 'a' or 'Z', a digit like '3' or '9', a space, a punctuation mark like '?' or '.', or a special symbol like '@' or '#'. Anything you can type on a keyboard is a character. Each character represents one unit of textual information, and it occupies exactly one position in memory.
It is important to distinguish a character from a string early on because they are two different things, even though they can look similar when written in code.
ch = 'h' # a single characters = "hello" # a string made up of 5 characters
A single ...