Python Reference

Every method. Every use case.

Home › Code Snippets › Python › String Methods

Complete Reference

Python String Methods

Every string method you need — split, join, strip, replace, format, and 20+ more. Each with a link to the full reference page, when to use it, and the common mistakes to avoid.

Last updated: October 2026 • Reading time: ~12 minutes

Quick answer: Use split() to break a string into a list, join() to combine a list into a string, strip() to remove whitespace from the ends, replace() to swap substrings, and f-strings for formatting. All string methods return new strings — Python strings are immutable.

🎯 The 5 methods you'll use daily

If you learn nothing else, learn these. They handle 90% of real string work.

Method Does Use when Returns
split() Break string into list Parsing text into tokens list
join() Combine list into string Building strings from parts str
strip() Remove chars from ends Cleaning user input str
replace() Swap substrings Find-and-replace anywhere str
f-string / format() Interpolate values Building messages, templates str

The most important rule:

Python strings are immutable. Every method returns a new string. None of them modify the original.

s = "  hello  "
s.strip()          # returns "hello"
print(s)           # still "  hello  "  ❌

s = s.strip()      # ✅ reassign
print(s)           # "hello"

🔀 Split and join — the conversion pair

String ↔ List. The most common operations in text processing.

🧹 Clean and trim methods

Remove whitespace or specific characters from the ends of a string.

🔁 Replace and translate

Swap substrings or single characters anywhere in a string.

🔠 Case conversion methods

Change the case of characters in a string.

✅ Check and test methods

Ask questions about a string. All return boolean.

🔍 Find and search methods

Locate a substring within a string.

📝 Format and interpolate methods

Build strings with values inserted. Three ways, one preferred.

⚖️ The comparisons that confuse everyone

These are the highest-search-volume Python string questions. Here's the definitive answer to each.

↔

split() vs join() — opposites, not alternatives

They do opposite things. You don't choose between them — you use the one that matches your direction.

# String → List
"a,b,c".split(",")
# ['a', 'b', 'c']

# List → String
",".join(['a', 'b', 'c'])
# 'a,b,c'

Remember: the separator is the string join is called on. separator.join(list), not list.join(separator).

🧹

strip() vs replace() — ends vs anywhere

strip only touches the ends. replace touches everywhere.

s = "  hello  "

# Remove from ends
s.strip()
# 'hello'

# s.replace() leaves ends alone, replaces anywhere
"  hello  ".replace(" ", "")
# 'hello'

If your string has whitespace in the middle that should stay, use strip. If you want to remove all spaces (or any substring) everywhere, use replace.

📝

f-strings vs .format() — which to use in 2026

Always f-strings for inline formatting. Use .format() only when the template is stored separately.

name = "Alice"

# f-string (preferred)
f"Hello, {name}!"
# 'Hello, Alice!'

# .format() (for templates)
template = "Hello, {name}!"
template.format(name=name)
# 'Hello, Alice!'

The only reason to use .format() is when the template lives in a file, database, or i18n string and gets formatted later with different values.

🔍

find() vs index() — safe vs loud

Same search, different failure mode.

"hello".find("x")      # -1 (safe, no exception)
"hello".index("x")     # ValueError (loud, breaks)

# Use find() when "not found" is normal
if "python" in title:
    i = title.find("python")

# Use index() when "not found" means broken
pos = code.index("=")  # if there's no =, that's a bug
✅

in vs find() — existence vs position

If you only need yes/no, use in. If you need where, use find().

# Just checking existence
if "world" in text:
    ...

# Need the position
i = text.find("world")
if i != -1:
    before = text[:i]
    after = text[i+5:]

🔒 The Python string rule

Every Python string method returns a new string. None of them modify the original. This is because Python strings are immutable.

# This does nothing useful
s = "hello"
s.upper()          # returns "HELLO" but s is still "hello"
print(s)           # "hello"

# You must reassign
s = "hello"
s = s.upper()
print(s)           # "HELLO"

# Or chain methods
result = "  Hello  ".strip().lower()
# 'hello'

Why immutable? It makes strings hashable (usable as dict keys), thread-safe, and lets Python cache common ones. The trade-off is that "modifying" a string always creates a new one — which is cheap for most cases but matters in tight loops.

⚠️ Common mistakes to avoid

❓ Frequently asked questions

What is the difference between split and join in Python?

split breaks a string into a list of substrings. join combines a list of strings into one string. They are opposites — split is String → List, join is List → String.

What is the difference between strip, lstrip, and rstrip in Python?

strip removes characters from both ends of a string. lstrip removes only from the left. rstrip removes only from the right. All three accept an optional chars argument to specify which characters to remove.

Should I use f-strings or .format() in Python?

Use f-strings (Python 3.6+) for inline interpolation — they are faster, more readable, and less error-prone. Use .format() only for templates stored separately from the values, or when you need a template string that gets reused with different arguments.

Is Python replace() case-sensitive?

Yes. str.replace() is case-sensitive. 'Hello'.replace('hello', 'Hi') returns 'Hello' unchanged. For case-insensitive replacement, use re.sub() with re.IGNORECASE or normalize both sides to lowercase first.

What is the difference between strip() and replace() in Python?

strip() removes leading and trailing characters only — it never touches the middle of a string. replace() replaces substrings anywhere, including in the middle.

Are Python string methods in-place or do they return new strings?

Python strings are immutable. Every string method returns a new string. None of them modify the original. You must assign the result: s = s.strip(), not just s.strip().

How do I check if a Python string contains a substring?

Use the 'in' operator: 'world' in 'hello world'. For case-insensitive checks, use .lower() on both sides. Or use str.find() or str.index() if you need the position.

📚 Related

← Python Hub