- Home
- /
- Computational Thinking
- /
- Programming Fundamentals
- /
- String
String
Also known as: text, str, character sequence
Grade 6-8
View on concept mapA data type that represents a sequence of characters (text), enclosed in quotation marks. Most programs work with text β user input, messages, file contents, and web pages are all strings.
Definition
A data type that represents a sequence of characters (text), enclosed in quotation marks. Strings support operations like concatenation (joining), slicing (extracting substrings), searching, and comparison, and can contain letters, digits, spaces, and special characters.
π‘ Intuition
A string is text in code β anything between quotes. Numbers inside quotes are text too: '42' is a string, not a number.
π― Core Idea
Strings are sequences β you can access individual characters by index, search for substrings, and combine strings.
Example
π Why It Matters
Most programs work with text β user input, messages, file contents, and web pages are all strings. Understanding string operations is essential because nearly every program reads, manipulates, and displays text.
π Hint When Stuck
When working with strings, remember that the + operator concatenates (joins) strings rather than adding them numerically. To combine text with numbers, convert the number to a string first. Use indexing to access individual characters, remembering that indices start at 0.
Formal View
Related Concepts
π§ Common Stuck Point
'123' is a string, not a number. You can't do math with it until you convert it: int('123') gives the number 123.
β οΈ Common Mistakes
- Using + between a string and a number without converting, causing a type error in strongly-typed languages
- Forgetting that string indices start at 0, so the first character is s[0] not s[1]
- Assuming strings are mutableβin many languages (Python, Java), strings cannot be changed in place; operations create new strings
Frequently Asked Questions
What is String in CS Thinking?
A data type that represents a sequence of characters (text), enclosed in quotation marks. Strings support operations like concatenation (joining), slicing (extracting substrings), searching, and comparison, and can contain letters, digits, spaces, and special characters.
When do you use String?
When working with strings, remember that the + operator concatenates (joins) strings rather than adding them numerically. To combine text with numbers, convert the number to a string first. Use indexing to access individual characters, remembering that indices start at 0.
What do students usually get wrong about String?
'123' is a string, not a number. You can't do math with it until you convert it: int('123') gives the number 123.
Prerequisites
Next Steps
How String Connects to Other Ideas
To understand string, you should first be comfortable with assignment and data types. Once you have a solid grasp of string, you can move on to boolean.