Word & Character Counter – Python Source Code

  • Code
  • Code With GUI
# Function to count words
def word_counter(text):
    words = text.split()  # Split the text into words using spaces
    return len(words)     # Return the count of words

# Function to count characters
def character_counter(text):
    return len(text)     # Return the total character count

# Get input from the user
input_text = input("Enter a sentence or paragraph: ")

# Call the function to diplay word count and character count
word_count = word_counter(input_text)
print("Word count:", word_count)

# Call the function to diplay character count
character_count = character_counter(input_text)
print("Letter Count:", character_count)

# Word And Character Counter - By Codotix
# Importing required module
import tkinter as tk

# Function to count words in the given text
def word_counter(text):
    words = text.split()
    return len(words)

# Function to count characters in the given text
def character_counter(text):
    return len(text)

# Function to update word and character counts
def update_counts():
    input_text = text_area.get("1.0", "end-1c")
    word_count = word_counter(input_text)
    character_count = character_counter(input_text)
    # Update labels with new counts
    word_count_label.config(text="Word Count: " + str(word_count))
    character_count_label.config(text="Character Count: " + str(character_count))

# Create the main application window
root = tk.Tk()
root.title("Word And Character Counter")

# Text input area
text_area = tk.Text(root, font="arial 14")
text_area.pack(fill=tk.BOTH, expand=True)

# Frame to hold count-related elements
count_frame = tk.Frame(root)
count_frame.pack(fill=tk.BOTH, pady=5)

# Button to start counting
count_button = tk.Button(count_frame, text="Count", font="8",
count_button.pack(side=tk.LEFT, padx=40)

# Label to display word count
word_count_label = tk.Label(count_frame, text="Word Count: 0", font="8")
word_count_label.pack(side=tk.LEFT, padx=80)

# Label to display character count
character_count_label = tk.Label(count_frame, text="Character Count: 0", font="8")
character_count_label.pack(side=tk.LEFT, padx=80)

# Start the main event loop

# Word And Character Counter - By Codotix

Leave a Comment

Your email address will not be published. Required fields are marked *