Guide Python Beginner

9.5 String Operators

Concatenation, repetition, membership testing, comparison, lexicographical ordering, iteration, and len() — the operators that work directly on strings without calling a method.

3 min read

Concatenation (+)

Joins two strings end-to-end into a new string — the simplest way to build text from pieces, used constantly for messages, file paths, and resource names built from variables.

>>> env, service = "prod", "api"
>>> env + "-" + service + "-server"
'prod-api-server'

Repetition (*)

Repeats a string a given number of times — commonly used for separators, indentation, or simple visual bars without a loop.

>>> "-" * 20
'--------------------'

Membership (in, not in)

Tests whether a substring occurs anywhere inside a string, returning True or False — the simplest way to check for a keyword’s presence without needing find()’s index.

>>> "a" in "Programming", "z" not in "Programming"
(True, True)

Comparison Operators

==, !=, <, >, <=, >= compare strings by their actual character content, not by identity (see 9.1 Introduction to Strings) — this is how exact matches are checked, such as validating a password or a config value.

>>> "password" == "password"
True
>>> "password" != "Password"     # comparison is case-sensitive
True

Lexicographical Comparison

Strings compare character by character using Unicode code point order (dictionary order for ASCII text) — this is what powers sorted() on lists of strings, covered generally in 7.4 Comparison Operators.

>>> "apple" < "banana"
True
>>> "Apple" < "apple"     # uppercase sorts before lowercase (A=65, a=97)
True

Iterating Through Strings

Because a string is an iterable sequence, a for loop visits each character in order — the basis for hand-written parsers, character counters, and validation logic.

>>> for ch in "abc":
...     print(ch, end=" ")
a b c

String Length (len())

len() returns the character count in O(1) time — the length is cached on the object, not recounted each call — used constantly for bounds checks, padding widths, and validation.

>>> len("Programming")
11

Quick Interview Answer

+ concatenates and * repeats, both producing new strings since strings are immutable. in/not in test substring membership. Comparison operators (==, <, …) compare content, never identity, using Unicode code-point order — the same order sorted() uses on a list of strings. len() is O(1) because the length is cached on the string object rather than recomputed. Iterating a string with for visits one character at a time, since str implements the iterable protocol.”

Common Mistakes

  • Using + to build a large string inside a loop — each += allocates an entirely new string, costing O(n²) overall; see 9.11 Memory and Performance for the join() alternative.
  • Assuming string comparison is case-insensitive — "Password" != "password"; normalize with .lower() first if case shouldn’t matter.
  • Forgetting uppercase letters sort before lowercase ones in code-point order, producing a surprising sort order on mixed-case data.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form