A string is a fundamental data type in Python used to represent a sequence of characters. It's how you store and manipulate text. Strings are created by enclosing characters in single quotes ('...'), double quotes ("..."), or triple quotes ('''...''' or """...""") for multi-line strings.
Key Characteristic: Immutability
The most important concept to understand about Python strings is that they are immutable. This means that once a string is created, it cannot be changed. Every operation or method that appears to modify a string (like converting it to uppercase or replacing a character) actually creates and returns a new string in memory, leaving the original unchanged.
Operations on Strings
- Concatenation (+): Combines two or more strings into a single string.
- Repetition (*): Creates a new string by repeating the original string a specified number of times.
- Indexing: Accesses a single character at a specific position (index) in the string. Python uses zero-based indexing, so the first character is at index 0.
- Slicing: Extracts a portion (a substring) of the string using a [start:stop:step] syntax.
- Membership Testing (in, not in): Checks if a substring exists within a string, returning True or False.
Built-in Functions for Strings
These are general Python functions that are very useful for working with strings.
- len(): Returns the number of characters in a string.
- str(): Converts other data types (like integers or floats) into a string.
- ord(): Returns the Unicode code point of a single character.
- chr(): Returns the character represented by a Unicode code point.
Common String Methods
Methods are functions that are called on a string object itself (e.g., my_string.upper()).
Case Conversion
- .lower(): Returns a new string with all characters converted to lowercase.
- .upper(): Returns a new string with all characters converted to uppercase.
- .capitalize(): Returns a new string with the first character capitalized and the rest lowercase.
- .title(): Returns a new string with the first character of each word capitalized.
Searching and Replacing
- .find(substring): Searches for a substring and returns the starting index of the first occurrence. Returns -1 if not found.
- .replace(old, new): Returns a new string where all occurrences of the old substring are replaced with the new substring.
- .startswith(prefix) / .endswith(suffix): Returns True if the string starts or ends with the specified substring.
- .count(substring): Returns the number of non-overlapping occurrences of a substring.
Splitting and Joining
- .split(separator): Splits the string into a list of substrings based on the given separator. If no separator is provided, it splits by whitespace.
- .join(iterable): A very useful method that joins the elements of an iterable (like a list) into a single string, with the string itself as the separator.
Whitespace and Formatting
- .strip(): Returns a new string with leading and trailing whitespace (spaces, tabs, newlines) removed. .lstrip() and .rstrip() remove only from the left or right.
- .format() / f-strings: Powerful ways to embed expressions inside string literals for formatting.
Character Checking
- .isalpha(): Returns True if all characters are alphabetic.
- .isdigit(): Returns True if all characters are digits.
- .isspace(): Returns True if all characters are whitespace.
# --- 1. String Creation ---
# Strings can be created with single, double, or triple quotes.
s1 = 'Hello, World!'
s2 = "Python Programming"
s3 = """This is a
multi-line string."""
# --- 2. Basic Operations ---
print("--- Basic Operations ---")
# Concatenation (+)
greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"
print(f"Concatenation: {message}")
# Repetition (*)
separator = "-" * 20
print(f"Repetition: {separator}")
# --- 3. Indexing and Slicing ---
# Accessing characters in a string.
text = "Python is fun"
print("\n--- Indexing and Slicing ---")
print(f"Original Text: '{text}'")
print(f"First character (index 0): {text[0]}") # 'P'
print(f"Last character (index -1): {text[-1]}") # 'n'
print(f"Slice from index 7 to 9: {text[7:10]}") # 'is '
# --- 4. Built-in Functions ---
print("\n--- Built-in Functions ---")
print(f"Length of text: {len(text)}")
num = 123
num_str = str(num)
print(f"Converting int to str: '{num_str}' (type: {type(num_str)})")
# --- 5. Case Conversion Methods ---
print("\n--- Case Conversion Methods ---")
mixed_case = "PyThOn Is CoOl"
print(f"Original: '{mixed_case}'")
print(f".lower(): {mixed_case.lower()}")
print(f".upper(): {mixed_case.upper()}")
print(f".capitalize(): {mixed_case.capitalize()}")
print(f".title(): {mixed_case.title()}")
# --- 6. Searching and Replacing Methods ---
sentence = "The quick brown fox jumps over the lazy dog."
print("\n--- Searching and Replacing Methods ---")
print(f"Original Sentence: '{sentence}'")
print(f".find('fox'): {sentence.find('fox')}") # Returns the starting index
print(f".find('cat'): {sentence.find('cat')}") # Returns -1 if not found
print(f".replace('dog', 'cat'): {sentence.replace('dog', 'cat')}")
print(f".startswith('The'): {sentence.startswith('The')}")
print(f".endswith('dog.'): {sentence.endswith('dog.')}")
print(f".count('the'): {sentence.lower().count('the')}") # Count case-insensitively
# --- 7. Splitting and Joining Methods ---
csv_data = "apple,banana,cherry,orange"
words = ["Python", "is", "awesome"]
print("\n--- Splitting and Joining Methods ---")
# .split() creates a list from a string
fruit_list = csv_data.split(',')
print(f".split(','): {fruit_list}")
# .join() creates a string from a list
joined_sentence = " ".join(words)
print(f"' '.join(...): '{joined_sentence}'")
# --- 8. Whitespace and Formatting Methods ---
whitespace_str = " some text with spaces "
print("\n--- Whitespace and Formatting ---")
print(f"Original: '{whitespace_str}'")
print(f".strip(): '{whitespace_str.strip()}'")
print(f".lstrip(): '{whitespace_str.lstrip()}'")
print(f".rstrip(): '{whitespace_str.rstrip()}'")
# f-string formatting (modern and preferred)
name = "Bob"
age = 30
print(f"f-string: My name is {name} and I am {age} years old.")
# --- 9. Character Checking Methods ---
str1 = "Python"
str2 = "12345"
str3 = "Python123"
print("\n--- Character Checking Methods ---")
print(f"'{str1}'.isalpha(): {str1.isalpha()}")
print(f"'{str2}'.isdigit(): {str2.isdigit()}")
print(f"'{str3}'.isalnum(): {str3.isalnum()}") # Checks for alphanumeric