Codectionary / Developer documentation / Python

String Methods

Strings in Python come with a rich set of built-in methods for transforming, searching, and inspecting text. Common ones include .upper()/.lower() for case conversion, .strip() for removing whitespace, .split() and .join() for converting between strings and lists, and .replace() for substitution. Since strings are immutable, every method returns a new string rather than modifying the original.

Syntax

string.method(arguments)

Examples

Case Conversion

Changing the case of a string for display or comparison purposes.

text = "Hello World"
print(text.upper())   # HELLO WORLD
print(text.lower())   # hello world
print(text.title())   # Hello World
print(text.swapcase()) # hELLO wORLD

Whitespace and Splitting

Cleaning up strings and breaking them into lists.

raw = "  Fola, Zain, Jamal  "
print(raw.strip())              # "Fola, Zain, Jamal"

names = raw.strip().split(", ")
print(names)                    # ['Fola', 'Zain', 'Jamal']

joined = " & ".join(names)
print(joined)                   # Fola & Zain & Jamal

Searching and Replacing

Finding substrings and performing text substitution.

text = "The quick brown fox"

print("quick" in text)         # True
print(text.find("brown"))      # 10 (index) or -1 if not found
print(text.replace("fox", "dog"))  # The quick brown dog
print(text.count("o"))         # 2

Validation Methods

Checking the format or content of a string.

print("12345".isdigit())    # True
print("hello".isalpha())    # True
print("hello123".isalnum()) # True
print("  ".isspace())       # True

email = "user@example.com"
print(email.startswith("user"))  # True
print(email.endswith(".com"))    # True

Best practices

  • Remember string methods return a new string - strings are immutable, so text.upper() does not modify text itself
  • Use .strip() on any user input before validating or comparing it, to remove accidental leading/trailing whitespace
  • Prefer 'substring in text' over text.find() when you just need a yes/no answer, since it's more readable
  • Chain methods when it improves readability (raw.strip().lower().split(',')), but break into steps if a chain gets too long

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

True
In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations. It's a fundamental keyword used throughout Python programming for decision-making, loops, comparisons, and boolean logic. True plays a key role in if statements, while loops, and logical expressions.
False
False is Python's built-in constant representing the boolean value false. It's the logical opposite of True and is essential for conditional logic, loop control, and boolean operations. False is used to indicate negative conditions, failed validations, or when something is not the case. Understanding False is crucial for writing effective control flow in Python programs.
None
None is Python's built-in singleton value that represents the absence of a value or a null result. It is its own type (NoneType) and is commonly used as a default parameter value, a placeholder before a variable is assigned real data, or a function's implicit return value when nothing is explicitly returned. Unlike 0, an empty string, or an empty list, None specifically means 'no value here', not 'an empty value'.
Variables & Assignment
Variables in Python are names bound to values, created the moment you first assign to them - there's no need to declare a type in advance, since Python is dynamically typed. A single variable can be reassigned to a completely different type later in the program. Python also supports several convenient assignment patterns, including assigning multiple variables at once and using augmented assignment operators.