String Utilities
The string utilities module provides helpful functions for string manipulation and processing.
Module Overview
- siege_utilities.core.string_utils.remove_wrapping_quotes_and_trim(target_string)[source]
Removes wrapping quotes (single or double) from a string and trims whitespace
- siege_utilities.core.string_utils.clean_string(target_string)[source]
Clean a string by removing quotes, trimming whitespace, and normalizing.
- siege_utilities.core.string_utils.normalize_whitespace(target_string)[source]
Normalize whitespace in a string by replacing multiple spaces with single space.
- Parameters:
target_string (str | None) – String to normalize
- Returns:
String with normalized whitespace
- Return type:
Examples
>>> normalize_whitespace("hello world") 'hello world' >>> normalize_whitespace("test\n\n\nvalue") 'test value'
- siege_utilities.core.string_utils.snake_case(text)[source]
Convert text to snake_case.
Examples
>>> snake_case("HelloWorld") 'hello_world' >>> snake_case("hello-world") 'hello_world' >>> snake_case("Hello World") 'hello_world'
- siege_utilities.core.string_utils.remove_non_alphanumeric(text, keep_chars='_-')[source]
Remove non-alphanumeric characters from text, optionally keeping specified chars.
- Parameters:
- Returns:
Cleaned text
- Return type:
Examples
>>> remove_non_alphanumeric("hello@world!") 'helloworld' >>> remove_non_alphanumeric("test_value-123", keep_chars="_-") 'test_value-123'
Functions
- siege_utilities.core.string_utils.remove_wrapping_quotes_and_trim(target_string)[source]
Removes wrapping quotes (single or double) from a string and trims whitespace
Usage Examples
Basic string processing:
import siege_utilities
# Remove wrapping quotes and trim whitespace
text = ' "Hello, World!" '
cleaned = siege_utilities.remove_wrapping_quotes_and_trim(text)
print(cleaned) # Output: Hello, World!
# Handle different quote types
text1 = '"Single quoted text"'
text2 = "'Double quoted text'"
text3 = '`Backtick quoted text`'
print(siege_utilities.remove_wrapping_quotes_and_trim(text1)) # Single quoted text
print(siege_utilities.remove_wrapping_quotes_and_trim(text2)) # Double quoted text
print(siege_utilities.remove_wrapping_quotes_and_trim(text3)) # Backtick quoted text
Real-world usage:
# Clean user input
user_input = ' "user@example.com" '
email = siege_utilities.remove_wrapping_quotes_and_trim(user_input)
# Process configuration values
config_values = [
'"production"',
"'development'",
' "staging" ',
'test'
]
cleaned_values = [
siege_utilities.remove_wrapping_quotes_and_trim(val)
for val in config_values
]
# Result: ['production', 'development', 'staging', 'test']
Unit Tests
The string utilities module has comprehensive test coverage:
✅ test_string_utils.py - All string utility tests pass
Test Coverage:
- Quote removal (single, double, backtick)
- Whitespace trimming
- Edge cases (no quotes, empty strings, mixed quotes)
- Performance with large strings
Test Results: All string utility tests pass successfully with 100% coverage.