Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Special Methods
Python Strings
Substring searching
Substring replacement involves finding a specific sequence of characters (a substring) within a larger string and then replacing it with a different sequence of characters. This is a common operation in text processing and data manipulation.
Python provides several methods for this purpose, with the most common being find(), rfind(), index(), and rindex().
1. find()
The find() method searches for the first occurrence of a specified substring and returns its index. If the substring is not found, it returns -1.
Syntax:
Python provides several methods for this purpose, with the most common being find(), rfind(), index(), and rindex().
1. find()
The find() method searches for the first occurrence of a specified substring and returns its index. If the substring is not found, it returns -1.
Syntax:
string.find(substring, start, end)
- substring: The substring you want to search for.
- start (optional): The starting index for the search.
- end (optional): The ending index for the search.
Example:
text = "Hello, welcome to Python programming." index = text.find("Python") print(index) # Output: 18
Search for a substring that doesn't exist:
not_found_index = text.find("Java") print(not_found_index) # Output: -1
2. rfind()
The rfind() method is similar to find(), but it searches for the last occurrence of the specified substring and returns its index. If the substring is not found, it also returns -1.
Syntax:
string.rfind(substring, start, end)
Example:
text = "Python is great. Python is versatile." last_index = text.rfind("Python") print(last_index) # Output: 22 (last occurrence) # Search for a substring that doesn't exist not_found_index = text.rfind("Java") print(not_found_index) # Output: -1
3. index()
The index() method works like find(), but instead of returning -1 when the substring is not found, it raises a ValueError.
Syntax:
string.index(substring, start, end)
Example:
text = "Hello, welcome to Python programming." index = text.index("welcome") print(index) # Output: 7 # Trying to find a substring that doesn't exist (will raise an error) try: not_found_index = text.index("Java") except ValueError as e: print(e) # Output: substring not found
4. rindex()
The rindex() method is similar to index(), but it searches for the last occurrence of the specified substring and raises a ValueError if the substring is not found.
Syntax:
string.rindex(substring, start, end)
Example:
text = "Python is great. Python is versatile." last_index = text.rindex("Python") print(last_index) # Output: 22 (last occurrence) # Trying to find a substring that doesn't exist (will raise an error) try: not_found_index = text.rindex("Java") except ValueError as e: print(e) # Output: substring not found
Summary of differences:
- find(): Returns the index of the first occurrence or -1 if not found.
- rfind(): Returns the index of the last occurrence or -1 if not found.
- index(): Returns the index of the first occurrence or raises a ValueError if not found.
- rindex(): Returns the index of the last occurrence or raises a ValueError if not found.
Additional considerations:
- Case Sensitivity: All these methods are case-sensitive, meaning "Python" and "python" are treated as different substrings.
- Slicing: You can use the `start` and `end` parameters to limit the search to a specific section of the string.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
Which Python method returns the index of the first occurrence of a substring, or -1 if it is not found?
Drindex()
Afind()
Brfind()
Cindex()
Check Answer
Which Python method searches for the last occurrence of a substring and returns its index, or -1 if not found?
Afind()
Brfind()
Cindex()
Drindex()
Check Answer
Which Python method raises a ValueError if a substring is not found?
Drindex()
Afind()
Brfind()
Cindex()
Check Answer
Which Python method returns the index of the last occurrence of a substring, and raises a ValueError if not found?
Afind()
Brfind()
Cindex()
Drindex()
Check Answer
Are the substring search methods in Python case-sensitive?
AYes
BNo
Check Answer