Intermediate Topic 19 of 54 Programming Fundamentals
Strings
Sequences of characters with operations like slicing, concatenation and search.
How it works
A string is a sequence of characters, immutable in many languages, so every edit creates a new string. Modern strings store text in an encoding such as UTF-8 or UTF-16, which means that one visible character can span several bytes or code units. Common operations are slicing, concatenation, searching, splitting, trimming, comparing and formatting.
Step-by-step walkthrough
- A string is a sequence of characters with an index each.
- s[1] reads the character e.
- Slicing s[1:4] gives ell.
- Concatenation builds a new string, because strings are usually immutable.
- Searching for "lo" scans along the characters.
Where it is used
Parsing text files, building messages, validating emails, generating HTML or SQL, searching logs and handling user names all rely on strings. Formatting functions and templates are preferred over manual concatenation.
Common pitfall
Concatenating in a loop is O(n squared) in some languages, so use join or a string builder. Mind Unicode versus byte length, case-insensitive comparison and locale rules. Building SQL or shell commands by pasting user text into strings enables injection attacks.
Where it fits in the course
Strings is topic 19 of 54 in Programming Fundamentals, rated Intermediate. It follows Arrays and Lists. The next topic is Dictionaries and Maps.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Programming Fundamentals quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Strings?
Where do people use Strings?
What is the most common mistake with Strings?
What should I learn before this topic?
How can I practice it?
Is Strings hard to learn?
How long does it take to learn Strings?
Which topics are related to Strings?
What should I study after Strings?
Related topics
- Arrays and Lists (Programming Fundamentals)
- Dictionaries and Maps (Programming Fundamentals)
- Debugging Basics (Programming Fundamentals)
- Sets (Programming Fundamentals)