Python Tutorial · Revision notes

Python Strings

A string is an ordered, immutable sequence of characters used to store text. This chapter covers creating strings, the most useful string methods, formatting with f-strings and slicing and indexing.

Quick revision

  • str is an ordered, immutable sequence of Unicode characters.
  • Quotes :single ,double ,triple for multi−line;r""for raw ;\n\tescapes .\text{Quotes}\ : \text{single}\ , \text{double}\ , \text{triple for}\ multi-line; r"" \text{for raw}\ ; \n \t \text{escapes}\ .
  • Methods return new strings: upper, lower, strip, replace, split, join, find, count, startswith.
  • find() gives -1 if missing; index() raises ValueError.
  • f"{value:.2f}" rounds; {n:,} adds commas; {x:>8} aligns; {x=} debugs.
  • Indexing starts at 0; negative index starts at -1 from the end.
  • s[start:stop:step]; stop is excluded; s[::-1] reverses; slices never raise IndexError.
  • Use "".join(list) instead of += in loops for speed.
Read the full Python Strings notes on Doubtclip →