Python Substrings
Creating a Substring Using Slicing
To create a substring using slicing, you can use the following syntax:
string[start:end]
Where string is the original string, start is the index of the first character in the substring, and end is the index of the character after the last character in the substring.
Here is an example:
original_string = "Hello, World!" substring = original_string[7:12] print(substring) # Output: "World"
In this example, we create a substring that includes the characters at index 7 through 11 (the last index is not included in the slice).
# Example 1: Get the first three characters of a string original_string = "Hello, World!" substring = original_string[0:3] print(substring) # Output: "Hel" # Example 2: Get the last three characters of a string original_string = "Hello, World!" substring = original_string[-3:] print(substring) # Output: "ld!" # Example 3: Get a substring from the middle of a string original_string = "Hello, World!" substring = original_string[6:9] print(substring) # Output: "Wor"
Creating a Substring Using Regex
To create a substring using a regular expression, you can use the re module and the search function.
Here is an example:
import re original_string = "Hello, World!" substring = re.search(r"Worl\w", original_string).group() print(substring) # Output: "World"
In this example, we use the regular expression r"Worl\w" to match the characters "Worl" followed by any alphanumeric character. The group function returns the matched substring.
# Example 1: Get the first word of a string import re original_string = "Hello, World!" substring = re.search(r"\b\w+\b", original_string).group() print(substring) # Output: "Hello" # Example 2: Get all digits in a string import re original_string = "There are 3 cats and 2 dogs." substring = re.findall(r"\d", original_string) print(substring) # Output: ["3", "2"] # Example 3: Get all words that start with a vowel import re original_string = "The quick brown fox jumps over the lazy dog." substring = re.findall(r"\b[aeiouAEIOU]\w+", original_string) print(substring) # Output: ["The", "over", "the"]
Checking for a Substring with the in Keyword
To check if a substring is present in a string, you can use the in keyword.
Here is an example:
original_string = "Hello, World!" substring = "World" if substring in original_string: print("The substring was found in the original string.") else: print("The substring was not found in the original string.")
# Example 1: Check if a string contains a specific substring original_string = "Hello, World!" substring = "World" if substring in original_string: print("The substring was found in the original string.") else: print("The substring was not found in the original string.") # Example 2: Check if a string starts with a specific substring original_string = "Hello, World!" substring = "Hello" if original_string.startswith(substring): print("The original string starts with the substring.") else: print("The original string does not start with the substring.") # Example 3: Check if a string ends with a specific substring original_string = "Hello, World!" substring = "!" if original_string.endswith(substring): print("The original string ends with the substring.") else: print("The original string does not end with the substring.")
Checking for a Substring with Regex
To check if a substring matches a regular expression, you can use the re module and the search function.
Here is an example:
import re original_string = "Hello, World!" substring = "Worl\w" if re.search(substring, original_string): print("The substring matches the regular expression.") else: print("The substring does not match the regular expression.")
In this example, we use the search function to check if the substring "Worl\w" (which is a regular expression) is present in the original string. If a match is found, the search function returns a match object; if no match is found, it returns None.
# Example 1: Check if a string contains a specific pattern import re original_string = "Hello, World!" substring = r"Worl\w" if re.search(substring, original_string): print("The substring matches the regular expression.") else: print("The substring does not match the regular expression.") # Example 2: Check if a string only contains alphanumeric characters import re original_string = "Hello, World!" substring = r"^[a-zA-Z0-9]+$" if re.search(substring, original_string): print("The original string only contains alphanumeric characters.") else: print("The original string contains non-alphanumeric characters.")
In this example, we use the regular expression r"^[a-zA-Z0-9]+$" to match a string that contains only alphanumeric characters. The ^ and $ characters are called "anchors" and match the start and end of the string, respectively. The [a-zA-Z0-9] character class matches any alphanumeric character, and the + quantifier specifies that one or more of these characters must be present.