Strings and text
Every program works with two kinds of data: numbers and text. You've covered numbers. Now meet the other one.
In Python, text is called a string.
What is a string?
A string is any sequence of characters wrapped in quotes:
Letters, spaces, punctuation, numbers — if it's wrapped in quotes, it's a string.
Single or double quotes
Python accepts both. Pick one and be consistent:
They're identical. Double quotes are more common, but single quotes are perfectly valid.
Strings can contain numbers
This is a common source of confusion early on:
They print the same — but Python treats them completely differently. One is a number you can do math with. The other is just text that happens to look like a number.
Measuring a string with len()
len() tells you how many characters are in a string:
Spaces count as characters. Punctuation counts too.
Key takeaways
- A string is any text wrapped in quotes
- Single and double quotes both work — pick one and stick with it
- A number inside quotes is a string, not a number — Python treats them differently
len()counts every character in a string, including spaces and punctuation
No comments yet. Add the first comment to start the discussion.