Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to test Typing Speed using Python?
Creating a typing speed test in Python is a fun way to measure and improve your typing skills. This tutorial will show you how to build a simple program that measures your words per minute (WPM) and accuracy.
Required Modules
We need two built-in Python modules for our typing speed test ?
import time import random
Setting Up Test Sentences
First, let's create a list of sentences to type during the test ?
sentences = [
"The quick brown fox jumps over the lazy dog.",
"To be or not to be, that is the question.",
"I have a dream that one day this nation will rise up.",
"Programming is the art of telling another human what one wants the computer to do.",
"Python is a great language for beginners and experts alike."
]
Calculating Words Per Minute
The standard formula divides character count by 5 (average word length) and divides by time in minutes ?
def calculate_wpm(time_taken, num_chars):
words = num_chars / 5 # Standard: 5 characters = 1 word
minutes = time_taken / 60
wpm = words / minutes
return wpm
def calculate_accuracy(original, typed):
correct_chars = sum(1 for i, char in enumerate(typed) if i < len(original) and char == original[i])
accuracy = (correct_chars / len(original)) * 100
return accuracy
Complete Typing Speed Test
Here's the complete program that runs a typing speed test ?
import time
import random
def calculate_wpm(time_taken, num_chars):
words = num_chars / 5
minutes = time_taken / 60
wpm = words / minutes
return wpm
def calculate_accuracy(original, typed):
correct_chars = sum(1 for i, char in enumerate(typed) if i < len(original) and char == original[i])
accuracy = (correct_chars / len(original)) * 100
return accuracy
sentences = [
"The quick brown fox jumps over the lazy dog.",
"To be or not to be, that is the question.",
"I have a dream that one day this nation will rise up.",
"Programming is the art of telling another human what one wants the computer to do.",
"Python is a great language for beginners and experts alike."
]
# Select a random sentence
sentence = random.choice(sentences)
print("Type the following sentence as quickly and accurately as possible:")
print(f"\n{sentence}\n")
print("Press Enter when ready to start...")
input()
print("Start typing NOW!")
start_time = time.time()
user_input = input()
end_time = time.time()
# Calculate results
time_taken = end_time - start_time
typing_speed = calculate_wpm(time_taken, len(sentence))
accuracy = calculate_accuracy(sentence, user_input)
# Display results
print(f"\nResults:")
print(f"Time taken: {time_taken:.2f} seconds")
print(f"Typing speed: {typing_speed:.2f} WPM")
print(f"Accuracy: {accuracy:.1f}%")
if accuracy > 95 and typing_speed > 40:
print("Excellent typing skills!")
elif accuracy > 90 and typing_speed > 30:
print("Good typing skills!")
else:
print("Keep practicing to improve your speed and accuracy!")
Type the following sentence as quickly and accurately as possible: The quick brown fox jumps over the lazy dog. Press Enter when ready to start... Start typing NOW! The quick brown fox jumps over the lazy dog. Results: Time taken: 8.45 seconds Typing speed: 62.13 WPM Accuracy: 100.0% Excellent typing skills!
Enhanced Version with Multiple Rounds
For a more comprehensive test, here's a version that runs multiple rounds ?
import time
import random
def typing_speed_test(rounds=3):
sentences = [
"The quick brown fox jumps over the lazy dog.",
"Python programming is fun and rewarding to learn.",
"Practice makes perfect in every skill you want to master.",
"Technology advances rapidly in our modern digital world.",
"Coding requires patience, logic, and continuous learning."
]
total_wpm = 0
total_accuracy = 0
for round_num in range(1, rounds + 1):
sentence = random.choice(sentences)
print(f"\n--- Round {round_num} of {rounds} ---")
print(f"Type: {sentence}")
input("Press Enter to start...")
start_time = time.time()
user_input = input("Start typing: ")
end_time = time.time()
time_taken = end_time - start_time
wpm = (len(sentence) / 5) / (time_taken / 60)
accuracy = (sum(1 for i, char in enumerate(user_input)
if i < len(sentence) and char == sentence[i]) / len(sentence)) * 100
total_wpm += wpm
total_accuracy += accuracy
print(f"Round {round_num}: {wpm:.1f} WPM, {accuracy:.1f}% accuracy")
avg_wpm = total_wpm / rounds
avg_accuracy = total_accuracy / rounds
print(f"\n--- Final Results ---")
print(f"Average Speed: {avg_wpm:.1f} WPM")
print(f"Average Accuracy: {avg_accuracy:.1f}%")
# Run the test
typing_speed_test(3)
Key Features
| Feature | Description | Purpose |
|---|---|---|
| WPM Calculation | Characters ÷ 5 ÷ minutes | Standard typing speed measurement |
| Accuracy Check | Correct characters / total characters | Measures typing precision |
| Random Sentences | Different text each test | Prevents memorization |
| Performance Feedback | Encouragement based on results | Motivates improvement |
Conclusion
This Python typing speed test measures both speed and accuracy using simple time tracking and string comparison. You can extend it further by adding difficulty levels, saving high scores, or creating a graphical interface using tkinter.
