FunStrings Examples

Practical examples showing how to use FunStrings in real-world scenarios.

Basic Usage
Simple examples of basic string operations
python
1import funstrings
2
3def main():
4 """Demonstrate the basic functionality of the FunStrings package."""
5
6 # Example string to work with
7 example = "Hello, World! This is a test string."
8
9 print("=" * 60)
10 print("FunStrings Package - Basic Usage Example")
11 print("=" * 60)
12 print(f"Original string: '{example}'")
13 print("-" * 60)
14
15 # Demonstrate string reversal
16 print(f"Reversed: '{funstrings.reverse_string(example)}'")
17
18 # Demonstrate case conversion
19 print(f"Uppercase: '{funstrings.to_upper(example)}'")
20 print(f"Lowercase: '{funstrings.to_lower(example)}'")
21
22 # Demonstrate counting functions
23 print(f"Vowel count: {funstrings.count_vowels(example)}")
24 print(f"Consonant count: {funstrings.count_consonants(example)}")
25 print(f"Word count: {funstrings.word_count(example)}")
26
27 # Demonstrate character sorting
28 print(f"Sorted chars: '{funstrings.sort_characters(example)}'")
29 print(f"Sorted (desc): '{funstrings.sort_characters(example, reverse=True)}'")
30
31 # Demonstrate whitespace removal
32 print(f"No whitespace: '{funstrings.remove_whitespace(example)}'")
33
34 # Demonstrate palindrome checking
35 print(f"Is palindrome: {funstrings.is_palindrome(example)}")
36
37if __name__ == "__main__":
38 main()
Text Analysis
Analyzing text content with FunStrings
python
1import funstrings
2
3def analyze_text(text):
4 """Analyze the given text and print various statistics."""
5 print(f"Text: '{text}'")
6 print(f"Word count: {funstrings.word_count(text)}")
7 print(f"Unique words: {funstrings.unique_words(text)}")
8 print(f"Most common word: {funstrings.most_common_word(text)}")
9 print(f"Word frequencies: {funstrings.get_word_frequencies(text)}")
10 print(f"Longest word: {funstrings.longest_word(text)}")
11 print(f"Shortest word: {funstrings.shortest_word(text)}")
12 print(f"Average word length: {funstrings.average_word_length(text)}")
13 print(f"Is pangram: {funstrings.is_pangram(text)}")
14
15# Example usage
16sample_text = "The quick brown fox jumps over the lazy dog. The fox is quick and brown."
17analyze_text(sample_text)
Data Cleaning
Clean and normalize text data
python
1import funstrings
2
3def clean_text(text):
4 """Clean and normalize the given text."""
5 # Remove HTML tags
6 text = funstrings.remove_html_tags(text)
7
8 # Remove emojis
9 text = funstrings.remove_emojis(text)
10
11 # Expand contractions
12 text = funstrings.expand_contractions(text)
13
14 # Correct whitespace issues
15 text = funstrings.correct_whitespace(text)
16
17 # Remove special characters if needed
18 # text = funstrings.remove_special_characters(text)
19
20 return text
21
22# Example usage
23dirty_text = "<p>Hello, <b>world</b>! 😊</p> This text has weird spacing and don't isn't properly formatted."
24cleaned_text = clean_text(dirty_text)
25print(f"Original: '{dirty_text}'")
26print(f"Cleaned: '{cleaned_text}'")
Pattern Extraction
Extract patterns from text
python
1import funstrings
2
3def extract_patterns(text):
4 """Extract various patterns from the given text."""
5 print(f"Text: '{text}'")
6 print(f"Numbers: {funstrings.extract_numbers(text)}")
7 print(f"Emails: {funstrings.extract_emails(text)}")
8 print(f"URLs: {funstrings.extract_urls(text)}")
9 print(f"Repeated words: {funstrings.find_repeated_words(text)}")
10
11# Example usage
12sample_text = "Contact us at info@example.com or support@example.org. Visit our website at https://example.com. Our phone number is 123-456-7890. The example website example is repeated."
13extract_patterns(sample_text)
14
15# Demonstrate masking sensitive data
16card_number = "1234-5678-9012-3456"
17masked_card = funstrings.mask_sensitive(card_number, 4)
18print(f"Original card: {card_number}")
19print(f"Masked card: {masked_card}")