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
Operation | Syntax | Example | Result | Description |
---|---|---|---|---|
String Literal | 'text' or "text" | s = "Hello" | "Hello" | Create string with quotes |
Multi-line String | """text""" | s = """Line 1\nLine 2""" | Multi-line | Create multi-line string |
Raw String | r'text' | r'C:\path\file' | 'C:\\path\\file' | Ignore escape sequences |
f-String | f'text {var}' | f'Hello {name}' | 'Hello John' | Formatted string literal |
Concatenation | str1 + str2 | "Hello" + " World" | "Hello World" | Join strings together |
Repetition | str * n | "Ha" * 3 | "HaHaHa" | Repeat string n times |
Membership | substr in str | "ell" in "Hello" | True | Check if substring exists |
Length | len(str) | len("Hello") | 5 | Get string length |
Indexing | str[i] | "Hello"[1] | 'e' | Access character at index |
Slicing | str[start:end] | "Hello"[1:4] | 'ell' | Extract substring |
Case Conversion Methods
Method | Purpose | Example | Result | Description |
---|---|---|---|---|
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
Method | Purpose | Example | Result | Description |
---|---|---|---|---|
find() | Find substring index | "hello".find("ll") | 2 | Returns index or -1 if not found |
rfind() | Find from right | "hello".rfind("l") | 3 | Find last occurrence |
index() | Find with exception | "hello".index("ll") | 2 | Like find() but raises ValueError if not found |
rindex() | Index from right | "hello".rindex("l") | 3 | Like rfind() but raises ValueError |
count() | Count occurrences | "hello".count("l") | 2 | Count non-overlapping occurrences |
startswith() | Check prefix | "hello".startswith("he") | True | Check if string starts with substring |
endswith() | Check suffix | "hello".endswith("lo") | True | Check if string ends with substring |
Character Testing Methods
Method | Purpose | Example | Result | Description |
---|---|---|---|---|
isalpha() | All alphabetic | "hello".isalpha() | True | All characters are letters |
isdigit() | All digits | "123".isdigit() | True | All characters are digits |
isalnum() | Alphanumeric | "hello123".isalnum() | True | All characters are letters or digits |
isspace() | All whitespace | " ".isspace() | True | All characters are whitespace |
islower() | All lowercase | "hello".islower() | True | All cased characters are lowercase |
isupper() | All uppercase | "HELLO".isupper() | True | All cased characters are uppercase |
istitle() | Title case | "Hello World".istitle() | True | String is in title case |
isdecimal() | Decimal characters | "123".isdecimal() | True | All characters are decimal |
isnumeric() | Numeric characters | "123".isnumeric() | True | All characters are numeric |
isascii() | ASCII characters | "hello".isascii() | True | All characters are ASCII |
isprintable() | Printable characters | "hello".isprintable() | True | All characters are printable |
isidentifier() | Valid identifier | "hello_world".isidentifier() | True | Valid Python identifier |
String Modification Methods
Method | Purpose | Example | Result | Description |
---|---|---|---|---|
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
Method | Purpose | Example | Result | Description |
---|---|---|---|---|
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
Method | Purpose | Example | Result | Description |
---|---|---|---|---|
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
Method | Purpose | Example | Result | Description |
---|---|---|---|---|
encode() | Encode to bytes | "hello".encode("utf-8") | b'hello' | Encode string to bytes |
decode() | Decode from bytes | b'hello'.decode("utf-8") | "hello" | Decode bytes to string |
String Formatting Methods
Method | Purpose | Example | Result | Description |
---|---|---|---|---|
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
Technique | Syntax | Example | Result | Description |
---|---|---|---|---|
% Formatting | "format" % values | "Hello %s" % "World" | "Hello World" | Old-style formatting |
str.format() | "{}".format(value) | "Hello {}".format("World") | "Hello World" | New-style formatting |
f-strings | f"text {variable}" | f"Hello {name}" | "Hello World" | Modern formatting (Python 3.6+) |
Format Specifiers
Specifier | Purpose | Example | Result | Description |
---|---|---|---|---|
{:d} | Integer | f"{42:d}" | "42" | Decimal integer |
{:f} | Float | f"{3.14159:.2f}" | "3.14" | Fixed-point number |
{:e} | Scientific | f"{1000:e}" | "1.000000e+03" | Scientific notation |
{:g} | General | f"{1000:g}" | "1000" | General format |
{:s} | String | f"{'hello':s}" | "hello" | String format |
{:x} | Hexadecimal | f"{255:x}" | "ff" | Lowercase hex |
{:X} | Hexadecimal | f"{255:X}" | "FF" | Uppercase hex |
{:o} | Octal | f"{8:o}" | "10" | Octal format |
{:b} | Binary | f"{5:b}" | "101" | Binary format |
{:%} | Percentage | f"{0.25:%}" | "25.000000%" | Percentage format |
Alignment and Padding in Format Strings
Format | Purpose | Example | Result | Description |
---|---|---|---|---|
{:<10} | Left align | f"{'hello':<10}" | "hello " | Left-align in 10 characters |
{:>10} | Right align | f"{'hello':>10}" | " hello" | Right-align in 10 characters |
{:^10} | Center align | f"{'hello':^10}" | " hello " | Center-align in 10 characters |
{:0>5} | Zero padding | f"{42:0>5}" | "00042" | Pad with zeros |
{:*^10} | Custom padding | f"{'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?
Track Your Learning Progress
Sign in to bookmark tutorials and keep track of your learning journey.
Your progress is saved automatically as you read.