String

Programming Fundamentals
definition

Also known as: text, str, character sequence

Grade 6-8

View on concept map

A 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

name = 'Alice'. greeting = 'Hello, ' + name gives 'Hello, Alice'. '3' + '4' gives '34' (string concatenation, not addition).

🌟 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

A string is a finite sequence of characters s = c_0 c_1 \ldots c_{n-1} from an alphabet \Sigma, where |s| = n is the string length and s[i] = c_i for 0 \leq i < n.

🚧 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.