A string is a sequence of characters used as text. In Python, its type is `str`. Put the characters inside matching single or double quotation marks: `"hello"` and `'hello'` are strings. So are `"525600"` (not a number) and `""` (an empty string). Spaces and symbols count as characters too. section 7.1
Want one character? Use an index in square brackets. Counting starts at 0, while `-1` means the last character. For example, `"Python rules!"[0]` gives `'P'`, and `"Python rules!"[-1]` gives `'!'`. A slice uses `[start:stop:step]` to retrieve a substring. section 7.2
Figure 7.1. The string “Python rules!” and the index of each character. The first row shows indexing with positive integers, and the second row shows indexing with negative integers.
Use the two rows of indices to see how the same characters can be counted from the beginning or the end. section 7.2
Once you have a string, try a few small operations:
- `len("Boston 4 ever")` gives `13`: spaces and symbols are included. section 7.3
- Use `.lower()` or `.upper()` to change letter case. Use `.find("ing")` to get a substring’s starting index, or `-1` if it is absent. section 7.3 section 8.1
- `"one" + "two"` gives `'onetwo'`; `3 * "a"` gives `'aaa'`. These operations combine strings or repeat one. section 8.2
Text from a user
`place = input("Where do you live? ")` stores what the user types as a string. `message = place.capitalize() + "!"` then changes its presentation and adds an exclamation mark. If you need a number for arithmetic instead, convert the input first: `num = int(input("Enter a number: "))`. section 11.2
Quick check: Is `"4"` the same type as `4`? No—the quotation marks make the first value a string. To practise further, try finding parts of two names and joining them into a new name. section 7.2 section 12.5























