Easy Text
py_simple.easy_text
Beginner-friendly helpers for common text formatting and cleaning.
capitalize_title(text)
Capitalizes the first letter of every word in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to capitalize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Text with every word capitalized. |
Example
from py_simple import capitalize_title
result = capitalize_title("the great gatsby") # -> "The Great Gatsby"
text = "the great gatsby"
result = text.title()
count_digits(text)
Counts the number of digits in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to count. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of digits (0-9). |
Example
from py_simple import count_digits
result = count_digits("Hello 123!") # -> 3
text = "Hello 123!"
result = sum(1 for c in text if c.isdigit())
count_letters(text)
Counts the number of letters in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to count. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of letters (a-z, A-Z, and accented letters). |
Example
from py_simple import count_letters
result = count_letters("Hello 123!") # -> 5
text = "Hello 123!"
result = sum(1 for c in text if c.isalpha())
extract_hashtags(text)
Extracts all hashtags from text, without the # symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to search. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Hashtag words found in the text. |
Example
from py_simple import extract_hashtags
result = extract_hashtags("Loving #python and #coding!")
# -> ["python", "coding"]
import re
text = "Loving #python and #coding!"
result = re.findall(r"#(\w+)", text)
mask_part(text, visible=4)
Hides part of text (like a card number) behind asterisks.
The first visible characters stay visible, the rest are masked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to mask. |
required |
visible
|
int
|
Number of characters to keep visible. Defaults to 4. |
4
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Masked text. |
Example
from py_simple import mask_part
result = mask_part("1234567890", 4) # -> "1234 ******"
text, visible = "1234567890", 4
result = text[:visible] + " " + "*" * len(text[visible:])
pluralize(word, count)
Returns the singular or plural form of a word based on the count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
word
|
str
|
Word to pluralize. |
required |
count
|
int
|
Number of items. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Singular word if count is 1, plural word otherwise. |
Example
from py_simple import pluralize
result = pluralize("cat", 3) # -> "cats"
word, count = "cat", 3
result = word if count == 1 else word + "s"
remove_punctuation(text)
Removes punctuation from text, keeping letters, numbers, and spaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to clean. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Text without punctuation. |
Example
from py_simple import remove_punctuation
result = remove_punctuation("Hello, world!") # -> "Hello world"
import string
text = "Hello, world!"
result = "".join(c for c in text if c not in string.punctuation)
reverse_words(text)
Reverses the order of words in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to reverse. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Text with words in reverse order. |
Example
from py_simple import reverse_words
result = reverse_words("Hello world") # -> "world Hello"
text = "Hello world"
result = " ".join(text.split()[::-1])
truncate(text, length)
Shortens text to the given length and adds an ellipsis character.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to shorten. |
required |
length
|
int
|
Maximum number of characters to keep. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Shortened text, or the original text if it is short enough. |
Example
from py_simple import truncate
result = truncate("Hello world!", 5) # -> "Hello…"
text, length = "Hello world!", 5
result = text[:length] + "…" if len(text) > length else text
word_frequency(text)
Counts how often each word appears in text.
Punctuation is ignored and words are counted in lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to analyze. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Word counts, keyed by word. |
Example
from py_simple import word_frequency
result = word_frequency("the cat and the dog")
# -> {"the": 2, "cat": 1, "and": 1, "dog": 1}
import re
from collections import Counter
text = "the cat and the dog"
words = re.sub(r"[^\w\s]", "", text).lower().split()
result = dict(Counter(words))