Python Substring

Substrings in Python are parts of strings extracted or manipulated using various methods. Strings are sequences of characters, so you can use indexing and slicing to work with substrings effectively. Let’s go step by step:

1. Accessing Characters with Indexing

Strings are sequences of characters, and each character has a specific index.

Positive Indexing

  • The first character starts at index 0.
  • The second character is at index 1, and so on.

Negative Indexing

  • The last character is at index -1.
  • The second-to-last character is at index -2, and so on.
text = "Python"

# Positive Indexing
print(text[0])  # First character
print(text[1])  # Second character

# Negative Indexing
print(text[-1])  # Last character
print(text[-2])  # Second-to-last character

Output:

P
y
n
o

2. Extracting Substrings with Slicing

The slicing syntax is:

string[start:stop:step]
  • start: the index where the substring starts, counting from 0.
  • stop: The index where the substring ends (exclusive). Default is the end of the string.
  • step: The stride, or step, between indices (default is 1).
text = "Python"

# Basic slicing
print(text[0:3])  # Characters from index 0 to 2
print(text[:4])   # Characters from the start to index 3
print(text[2:])   # Characters from index 2 to the end

# Step slicing
print(text[::2])  # Every second character
print(text[::-1]) # Reverse the string

Output:

Pyt
Pyth
thon
Pto
nohtyP

3. Substring Membership

You can check if a substring exists in a string using the in keyword.

text = "Python programming"

# Check for substring
print("Python" in text)  # True
print("Java" in text)    # False

Output:

True
False

4. Extracting Substrings Dynamically

Using find()

  • Finds the first occurrence of a substring.
  • Returns -1 if the substring is not found.
text = "Python programming"

print(text.find("prog"))  # Index of "prog"
print(text.find("Java"))  # -1 (not found)

Output:

7
-1

Using index()

  • Similar to find(), but raises a ValueError if the substring is not found.
text = "Python programming"

print(text.index("prog"))  # Index of "prog"
# print(text.index("Java"))  # Uncommenting this will raise ValueError

Output:

7

5. Using Regular Expressions (re)

Regular expressions allow advanced pattern matching.

import re

text = "Python programming is fun!"

# Find words starting with 'pro'
match = re.findall(r"pro\w+", text)
print(match)

Output:

['programming']

6. Useful String Methods

startswith() and endswith()

  • Check if a string starts or ends with a specific substring.
text = "Python programming"

print(text.startswith("Python"))  # True
print(text.endswith("ing"))       # True

Output:

True
True

split()

  • Splits a string into a list of substrings based on a delimiter.
text = "apple,banana,cherry"

result = text.split(",")
print(result)

Output:

['apple', 'banana', 'cherry']

join()

  • Joins a list of strings into a single string.
fruits = ['apple', 'banana', 'cherry']

result = ", ".join(fruits)
print(result)

Output:

apple, banana, cherry

replace()

  • Replaces all occurrences of a substring with another.
text = "I love Python"

result = text.replace("love", "like")
print(result)

Output:

I like Python

strip()

  • Removes leading and trailing characters (default is whitespace).
text = "  Python  "

result = text.strip()
print(result)

Output:

Python

7. Common Substring Problems

Reverse a String

text = "Python"

reversed_text = text[::-1]
print(reversed_text)

Output:

nohtyP

Extract a Specific Substring

Extract the domain from an email address:

email = "user@example.com"

domain = email[email.index("@") + 1:]
print(domain)

Output:

example.com

Check for Palindrome

Check if a string reads the same backward as forward:

text = "madam"

is_palindrome = text == text[::-1]
print(is_palindrome)

Output:

True

8. Handling Substrings Safely

Example

Use find() or membership checks to avoid errors when a substring might not exist.

text = "Hello"

if "H" in text:
    print("Found!")

Output:

Found!