String Operations

Complete reference for Python string methods and operations. Strings in Python are immutable sequences of Unicode characters with extensive built-in functionality for text processing and manipulation.

String Creation and Basic Operations

OperationSyntaxExampleResultDescription
String Literal'text' or "text"s = "Hello""Hello"Create string with quotes
Multi-line String"""text"""s = """Line 1\nLine 2"""Multi-lineCreate multi-line string
Raw Stringr'text'r'C:\path\file''C:\\path\\file'Ignore escape sequences
f-Stringf'text {var}'f'Hello {name}''Hello John'Formatted string literal
Concatenationstr1 + str2"Hello" + " World""Hello World"Join strings together
Repetitionstr * n"Ha" * 3"HaHaHa"Repeat string n times
Membershipsubstr in str"ell" in "Hello"TrueCheck if substring exists
Lengthlen(str)len("Hello")5Get string length
Indexingstr[i]"Hello"[1]'e'Access character at index
Slicingstr[start:end]"Hello"[1:4]'ell'Extract substring

Case Conversion Methods

MethodPurposeExampleResultDescription
upper()Convert to uppercase"hello".upper()"HELLO"All characters to uppercase
lower()Convert to lowercase"HELLO".lower()"hello"All characters to lowercase
capitalize()Capitalize first letter"hello world".capitalize()"Hello world"First character uppercase, rest lowercase
title()Title case"hello world".title()"Hello World"First letter of each word uppercase
swapcase()Swap case"Hello World".swapcase()"hELLO wORLD"Uppercase to lowercase and vice versa
casefold()Aggressive lowercase"HELLO".casefold()"hello"More aggressive than lower()

Text Searching and Testing Methods

MethodPurposeExampleResultDescription
find()Find substring index"hello".find("ll")2Returns index or -1 if not found
rfind()Find from right"hello".rfind("l")3Find last occurrence
index()Find with exception"hello".index("ll")2Like find() but raises ValueError if not found
rindex()Index from right"hello".rindex("l")3Like rfind() but raises ValueError
count()Count occurrences"hello".count("l")2Count non-overlapping occurrences
startswith()Check prefix"hello".startswith("he")TrueCheck if string starts with substring
endswith()Check suffix"hello".endswith("lo")TrueCheck if string ends with substring

Character Testing Methods

MethodPurposeExampleResultDescription
isalpha()All alphabetic"hello".isalpha()TrueAll characters are letters
isdigit()All digits"123".isdigit()TrueAll characters are digits
isalnum()Alphanumeric"hello123".isalnum()TrueAll characters are letters or digits
isspace()All whitespace" ".isspace()TrueAll characters are whitespace
islower()All lowercase"hello".islower()TrueAll cased characters are lowercase
isupper()All uppercase"HELLO".isupper()TrueAll cased characters are uppercase
istitle()Title case"Hello World".istitle()TrueString is in title case
isdecimal()Decimal characters"123".isdecimal()TrueAll characters are decimal
isnumeric()Numeric characters"123".isnumeric()TrueAll characters are numeric
isascii()ASCII characters"hello".isascii()TrueAll characters are ASCII
isprintable()Printable characters"hello".isprintable()TrueAll characters are printable
isidentifier()Valid identifier"hello_world".isidentifier()TrueValid Python identifier

String Modification Methods

MethodPurposeExampleResultDescription
strip()Remove whitespace" hello ".strip()"hello"Remove leading/trailing whitespace
lstrip()Remove left whitespace" hello ".lstrip()"hello "Remove leading whitespace
rstrip()Remove right whitespace" hello ".rstrip()" hello"Remove trailing whitespace
replace()Replace substring"hello".replace("l", "x")"hexxo"Replace occurrences of substring
removeprefix()Remove prefix"hello".removeprefix("he")"llo"Remove prefix if present (Python 3.9+)
removesuffix()Remove suffix"hello".removesuffix("lo")"hel"Remove suffix if present (Python 3.9+)

String Splitting and Joining Methods

MethodPurposeExampleResultDescription
split()Split string"a,b,c".split(",")['a', 'b', 'c']Split by delimiter into list
rsplit()Split from right"a.b.c".rsplit(".", 1)['a.b', 'c']Split from right with max splits
splitlines()Split by lines"a\nb\nc".splitlines()['a', 'b', 'c']Split by line breaks
partition()Partition string"a-b-c".partition("-")('a', '-', 'b-c')Split into 3 parts at first separator
rpartition()Partition from right"a-b-c".rpartition("-")('a-b', '-', 'c')Split into 3 parts at last separator
join()Join sequence",".join(['a', 'b', 'c'])"a,b,c"Join sequence elements with string

String Alignment and Padding Methods

MethodPurposeExampleResultDescription
center()Center align"hello".center(10)" hello "Center string in given width
ljust()Left align"hello".ljust(10)"hello "Left-align string in given width
rjust()Right align"hello".rjust(10)" hello"Right-align string in given width
zfill()Zero padding"42".zfill(5)"00042"Pad with zeros on the left
expandtabs()Expand tabs"a\tb".expandtabs(4)"a b"Replace tabs with spaces

String Encoding and Decoding Methods

MethodPurposeExampleResultDescription
encode()Encode to bytes"hello".encode("utf-8")b'hello'Encode string to bytes
decode()Decode from bytesb'hello'.decode("utf-8")"hello"Decode bytes to string

String Formatting Methods

MethodPurposeExampleResultDescription
format()Format string"Hello {}".format("World")"Hello World"Insert values into string
format_map()Format with mapping"Hello {name}".format_map({'name': 'World'})"Hello World"Format using dictionary

Advanced String Operations

String Formatting Techniques

TechniqueSyntaxExampleResultDescription
% Formatting"format" % values"Hello %s" % "World""Hello World"Old-style formatting
str.format()"{}".format(value)"Hello {}".format("World")"Hello World"New-style formatting
f-stringsf"text {variable}"f"Hello {name}""Hello World"Modern formatting (Python 3.6+)

Format Specifiers

SpecifierPurposeExampleResultDescription
{:d}Integerf"{42:d}""42"Decimal integer
{:f}Floatf"{3.14159:.2f}""3.14"Fixed-point number
{:e}Scientificf"{1000:e}""1.000000e+03"Scientific notation
{:g}Generalf"{1000:g}""1000"General format
{:s}Stringf"{'hello':s}""hello"String format
{:x}Hexadecimalf"{255:x}""ff"Lowercase hex
{:X}Hexadecimalf"{255:X}""FF"Uppercase hex
{:o}Octalf"{8:o}""10"Octal format
{:b}Binaryf"{5:b}""101"Binary format
{:%}Percentagef"{0.25:%}""25.000000%"Percentage format

Alignment and Padding in Format Strings

FormatPurposeExampleResultDescription
{:<10}Left alignf"{'hello':<10}""hello "Left-align in 10 characters
{:>10}Right alignf"{'hello':>10}"" hello"Right-align in 10 characters
{:^10}Center alignf"{'hello':^10}"" hello "Center-align in 10 characters
{:0>5}Zero paddingf"{42:0>5}""00042"Pad with zeros
{:*^10}Custom paddingf"{'hello':*^10}""**hello***"Pad with custom character

Common String Operations Examples

Text Processing

# Clean and normalize text
text = "  Hello, World!  "
cleaned = text.strip().lower().replace(",", "")
print(cleaned)  # "hello world!"
# Extract file extension
filename = "document.pdf"
name, ext = filename.rsplit(".", 1)
print(f"Name: {name}, Extension: {ext}")

Data Validation

# Validate input
def is_valid_email(email):
    return "@" in email and "." in email.split("@")[-1]

# Test validation
print(is_valid_email("user@example.com"))  # True
print(is_valid_email("invalid.email"))     # False

String Building

# Efficient string building
parts = ["Hello", "beautiful", "world"]
result = " ".join(parts)
print(result)  # "Hello beautiful world"
# Template formatting
template = "Hello {name}, you have {count} messages"
message = template.format(name="Alice", count=5)
print(message)

Text Analysis

# Word frequency
text = "hello world hello python world"
words = text.split()
word_count = {word: words.count(word) for word in set(words)}
print(word_count)  # {'hello': 2, 'world': 2, 'python': 1}

# Extract numbers from text
import re
text = "The price is $25.99 and tax is $3.50"
prices = [float(match) for match in re.findall(r'\d+\.\d+', text)]
print(prices)  # [25.99, 3.5]

Performance Tips

  • Use join() for multiple concatenations: "".join(parts) is faster than repeated +
  • Use f-strings for formatting: Generally fastest formatting method
  • Use str methods instead of regex: Built-in methods are often faster for simple operations
  • Cache compiled regex patterns: If using regex repeatedly
  • Use string constants: For repeated string literals
  • Consider str.translate(): For character replacement operations

Was this helpful?

😔Poor
🙁Fair
😊Good
😄Great
🤩Excellent