Strings and their length
A string is text. It is a row of characters inside quotation marks, single or double, as long as the two marks match. The characters can be letters, digits, symbols and spaces, not letters only. So "school", "2026" and "hi there!" are all strings. The function len() counts the characters: len("school") is 6. Spaces count too, so len("good day") is 8 and an empty string "" has length 0. Take care with digits inside quotation marks: "12" is text, not a number. Plus on two strings joins them, so "12" + "3" gives 123, while 12 + 3 gives 15. Remember Week 1, where the typed input was text until int() changed it.
| String | Characters inside | len |
|---|---|---|
| "Python" | 6 letters | 6 |
| "a1!" | a letter, a digit, a symbol | 3 |
| "hi there" | 7 letters and 1 space | 8 |
| "2026" | 4 digits | 4 |
1word = "school" 2print(len(word)) 3print(len("good day")) 4print(len(""))
6 8 0
1print("12" + "3") 2print(12 + 3)
123 15
Write three strings of your own: one with only letters, one with letters and digits, and one with a space and a symbol. Write the length of each, counting every character. Check one with a classmate.