Python Tutorial / Work with Strings, Slices and Formatting
A string is an ordered text value that supports indexing and slicing. Strings are immutable, so methods such as strip() and replace() return a result that you must use or assign rather than changing the original string.
Identify positions and slice boundaries
Index zero refers to the first character; index -1 refers to the last. A slice text[start:stop] stops before stop. An out-of-range individual index raises an error, while a slice returns the available portion. String length counts Unicode code points, not bytes; a visible character or emoji may contain several code points.
text = "Python" print(text[0], text[-1], text[1:4])
This prints P n yth. Establishing whether the end is included prevents common off-by-one mistakes.
For the relevant APIs and platform requirements, see Python: stdtypes.
Structured text and SQL values build on the rules demonstrated in Read and Write CSV Files.
Clean and format a complete input
Save the complete code below as strings.py in a separate practice folder, using UTF-8. Run python strings.py in a terminal with your project virtual environment selected. Outside a virtual environment on macOS or Ubuntu, use python3 when that is your interpreter command.
raw = " lunch,8000 "
clean = raw.strip()
name, amount_text = clean.split(",")
amount = int(amount_text)
print(clean)
print(name.upper())
print(f"{name}: {amount:,} KRW")
print("-".join(["2026", "10", "17"]))
Expected output:
lunch,8000 LUNCH lunch: 8,000 KRW 2026-10-17
Choose splitting, joining and formatting tools
split() divides text into a list; join() combines strings with a separator. Every item passed to join() must be a string, so convert numeric items deliberately. For CSV data with quoted commas, use the csv module rather than assuming split(",") can parse the format.
An f-string is useful for readable output. It should not interpolate external input into SQL statements. Human-facing formatting and database parameter binding solve different problems; the MariaDB access lesson shows parameters in context.
split(",") does not interpret quoted commas or multiline CSV fields. Use the csv module for CSV and query parameters for SQL values. Splitting text is a different task from parsing a file format or constructing a database query safely.
Preserve the original and test edge cases
Calling raw.strip() without keeping the returned value leaves raw unchanged; replace() behaves similarly. Names such as raw and clean make that distinction visible. Assign the returned value when replacement is intentional.
- Add surrounding spaces and verify that clean remains the same.
- Remove the comma and observe that unpacking into two variables fails, then restore it.
- Try assigning to text[0] and observe TypeError because a string cannot be modified in place.
These operations clean a known format; they do not establish that arbitrary user data is valid. Separate text normalization from checks on the number and meaning of fields.




