How it works
Call a method with parentheses, such as text.strip(). Store its return value if you want to keep the result. strip() removes leading and trailing whitespace by default, not whitespace in the middle. strip(chars) treats chars as a set of characters to remove, not a complete prefix or suffix.
split() without a separator groups runs of whitespace and drops empty edge fields. split(",") uses a literal separator and preserves empty fields. join() is called on the separator, for example ", ".join(names), and requires string items.
Use in when you only need to know whether a substring exists. find() gives its first index or -1 if absent; testing if text.find(...) is wrong because index 0 is falsy and -1 is truthy. casefold() is useful for Unicode caseless matching, although a complete matching policy may also need normalisation.
Common mistakes and boundaries
Do not assign the result of every string method expecting another string. Inspect the documented return type.
Character tests such as isdigit() are not complete number parsers or validators for names, emails or passwords. In particular, some Unicode digits pass isdigit() but cannot be parsed with int().
Strip only when whitespace is not meaningful for your data. Do not silently normalise secrets or text where exact characters matter.
Try it yourself
Compare "a,,b".split(",") with " a b ".split(). Then search for a word at index zero and one that is absent. Write a presence check that handles both correctly.
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- Each method returns a transformed string. The original text is still "Hello World" after all four calls.
- Expected output: HELLO WORLD hello world Hello World hELLO wORLD
Whitespace and Splitting
Cleaning up strings and breaking them into lists.
raw = " John, Zain, Jamal "
print(raw.strip()) # "John, Zain, Jamal"
names = raw.strip().split(", ")
print(names) # ['John', 'Zain', 'Jamal']
joined = " & ".join(names)
print(joined) # John & Zain & Jamal- strip cleans the outer whitespace, split creates a list of names, and join builds one new string from that list.
- Expected output: John, Zain, Jamal ['John', 'Zain', 'Jamal'] John & 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- Membership returns a Boolean; find returns an index; replace returns new text; count returns an integer.
- Expected output: True 10 The quick brown dog 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- These methods inspect characters or prefixes/suffixes only. The last two checks do not establish that an email address is valid or deliverable.
- Expected output: True True True True True True
Best practices
- Store transformation results when you need them; strings are immutable.
- Use in for substring presence and find only when you need its index.
- Choose split() or an explicit separator according to the input format.
- Use int() with error handling to parse integers instead of relying on character tests.
Runtime & versions
Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Python language reference