← Back to Python Course | Chapter 5: Strings | Lesson 6 of 6

Python String Operations

String operations are things you can do with text, like gluing two words together, repeating one, or checking whether one word is inside another. It is like playing with word blocks.
Syntax
python
string1 + string2
string * n
substring in string
len(string)

String Concatenation

The + operator joins strings end to end into a new string; because strings are immutable in Python, concatenating in a loop repeatedly creates new objects, which is why joining many pieces with ''.join(list) is the more efficient idiom for large amounts of text.

Example: String Concatenation

python
first = "Hello"
second = "World"
print(first + " " + second)  # + joins the strings end to end into a new string

String Repetition

Multiplying a string by an integer with * repeats it that many times, which is a quick way to build separators, padding, or ASCII-art borders like '-' * 40 without writing a loop.

Example: String Repetition

python
print("-" * 10)

Membership Operators

in and 'not in' check for substring presence and return a boolean, making them the simplest way to test membership before doing more expensive parsing -- for example checking if '@' in email before attempting to validate an address further.

Example: Membership Operators

python
email = "[email protected]"
print("@" in email)

Comparing Strings

Comparison operators like < and > compare strings character by character using Unicode code points, so Apple < apple is True because uppercase letters sort before lowercase ones -- a frequent source of bugs when sorting user input without normalizing case first.

Example: Comparing Strings

python
print("Apple" < "apple")

Iterating over Strings

Looping over a string with a for loop visits one character at a time in order, which is useful for tasks like counting vowels or building a character-frequency map without needing index-based access via range(len(s)).

Example: Iterating over Strings

python
for char in "cat":
    print(char)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Concatenating a string and a number with +, as in "Age: " + 25, which raises TypeError; use str(25) or an f-string.
  2. Building a large string with += in a long loop instead of "".join(parts), which is slower because each step creates a new string.
  3. Comparing strings with different case, as in "apple" == "Apple", which is False; normalise with .lower() first.
Chapter Summary
  • Strings have many methods and support slicing and formatting.
  • Regular expressions match patterns in text.
  • String operations combine and manipulate text.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.