TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 5 of 22 · By Ethan Cole · Updated November 21, 2026

Python Strings: Indexing, Slicing and Methods

A string (str) is a sequence of characters. You can write one with single quotes, double quotes, or triple quotes for text that spans several lines. 'cat' and "cat" are the same value; pick whichever lets you avoid escaping, such as "It's".

Indexing and slicing

Each character has a position, starting at 0. word[0] is the first character, and negative indexes count from the end, so word[-1] is the last. A slice word[start:stop] returns the characters from start up to, but not including, stop. Leave out start to begin at the front or stop to run to the end. Slicing never raises an error for out-of-range positions; it just returns what exists.

Strings are immutable: you cannot change a character in place. Every method that seems to modify a string actually returns a new one, so remember to assign the result.

Python 3 · editable

Expected output

P n
Pyt hon
6
PYTHON python
padded!
['a', 'b', 'c']
2026-11-21
True PyTHon

Methods worth memorizing

  • upper(), lower() and title() change letter case.
  • strip() removes whitespace from both ends; lstrip() and rstrip() do one side.
  • split(sep) breaks a string into a list of pieces. With no argument it splits on any run of whitespace.
  • sep.join(items) is the reverse: it glues a list of strings together with sep between them. Note that the separator comes first.
  • replace(old, new) substitutes every occurrence.
  • startswith(), endswith() and find() search without changing anything.

The in operator checks for a substring: "th" in "Python" is True. Use len() to count characters. For a full list with examples, see our Python string methods cheat sheet.

Escape sequences

Inside quotes a backslash starts an escape: \n is a new line, \t a tab and \\ a literal backslash. Prefix a string with r, as in r"C:\new", to make a raw string in which backslashes are kept as typed. Raw strings are common for Windows paths and regular expressions.

Common mistakes

  • Calling text.upper() and expecting text to change. Write text = text.upper().
  • Off-by-one slices: word[0:3] gives three characters, positions 0, 1 and 2.
  • Writing ",".join backwards as list.join(","). Lists have no join method.

Summary

  • Indexes start at 0; negative indexes count from the end.
  • s[a:b] slices from a up to but not including b.
  • Strings are immutable; methods return new strings.
  • split() and join() convert between strings and lists.

Quiz: Strings

1. What is "Python"[1:4]?

The slice starts at index 1 (y) and stops before index 4, giving yth.

2. What does "a-b-c".split("-") return?

split returns a list of the pieces between separators.

3. After s = "hi" and s.upper(), what is s?

Strings are immutable; upper() returns a new string that was not assigned to anything.

4. Which expression joins ["2026", "11", "21"] into "2026-11-21"?

join is a string method called on the separator.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.