Python String Helper Functions for Beginners

A set of beginner-friendly Python functions that count words, reverse text, convert to title case, strip punctuation, and compute character frequency from a string.

Easy Python 3.9+ Aug 9, 2026 Strings & text 14 views 0 copies

Python code

35 lines
Python 3.9+
def count_words(text):
    """Count the number of words in a string."""
    return len(text.split())


def reverse_text(text):
    """Reverse the entire string."""
    return text[::-1]


def title_case(text):
    """Capitalize the first letter of each word."""
    return text.title()


def remove_punctuation(text):
    """Remove common punctuation marks from text."""
    import string
    return text.translate(str.maketrans("", "", string.punctuation))


def get_char_frequency(text):
    """Return a dictionary of character counts (ignoring spaces)."""
    text = text.replace(" ", "").lower()
    return {char: text.count(char) for char in set(text)}


if __name__ == "__main__":
    sample = "Hello, World! Python is fun."
    print("Original:", sample)
    print("Words:", count_words(sample))
    print("Reversed:", reverse_text(sample))
    print("Title case:", title_case(sample))
    print("No punctuation:", remove_punctuation(sample))
    print("Char frequency:", get_char_frequency(sample))

Output

stdout
Original: Hello, World! Python is fun.
Words: 5
Reversed: .nuf si nohtyP !dlroW ,olleH
Title case: Hello, World! Python Is Fun.
No punctuation: Hello World Python is fun
Char frequency: {'h': 1, 'e': 1, 'l': 3, 'o': 3, 'w': 1, 'r': 1, 'd': 1, 'p': 1, 'y': 1, 't': 1, 'n': 2, 'i': 2, 's': 1, 'f': 1, 'u': 1}

How it works

The split() method on strings divides text by whitespace by default, so len() gives the word count. Slicing with [::-1] reverses a string because the negative step walks backward through the sequence. .title() capitalizes the first letter of every word while lowercasing the rest. str.maketrans with string.punctuation builds a translation table that deletes punctuation characters via translate. The character frequency uses a set comprehension to iterate unique characters and counts each with .count(), after removing spaces and lowercasing.

Common mistakes

  • Forgetting to handle empty strings, which return 0 words and an empty frequency dict
  • Using `.title()` on abbreviations or mixed-case text, which lowercases the rest of each word
  • Not removing punctuation before counting words, leading to inflated counts
  • Calling `count()` in a loop over every character instead of using `Counter` for better performance on large text

Variations

  1. Use `collections.Counter(text)` to count characters in one pass instead of looping with `.count()`
  2. Combine punctuation removal and lowercasing in a single `re.sub(r'[^\w\s]', '', text)` regex

Real-world use cases

  • Building a quick text analyzer for a CLI tool that reports word counts and readability stats.
  • Preparing user-generated comments for keyword tagging by normalizing case and removing punctuation.
  • Powering a search index preprocessor that strips noise characters before tokenizing documents.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Strings & text

Related tutorials and quizzes for this topic.