Home Chapters Projects About
HomeProjects › Day 59
DAY 59 — 60 DAY PYTHON CHALLENGE

Phone Number Formatter

Python mein Phone Number Formatter banao — Hindi mein step by step

Advertisement

Project kya hai?

Day 59 ka project hai Phone Number Formatter! Yeh project banane se aap Python ke important concepts practice kar payenge. Neeche complete code hai — copy karo aur chalao!

Complete Python Code

python — phone_fmt.py
import re

def format_phone(number):
    # Sirf digits rakhein
    digits = re.sub(r'[^0-9]', '', number)

    if len(digits) == 10 and digits[0] in "6789":
        return f"+91 {digits[:5]}-{digits[5:]}", "Valid Indian Mobile"
    elif len(digits) == 11 and digits[0] == "0":
        d = digits[1:]
        if d[0] in "6789":
            return f"+91 {d[:5]}-{d[5:]}", "Valid (0 prefix)"
    elif len(digits) == 12 and digits[:2] == "91":
        d = digits[2:]
        if d[0] in "6789":
            return f"+91 {d[:5]}-{d[5:]}", "Valid (91 prefix)"
    elif len(digits) < 10:
        return number, "Invalid - bahut chhota!"
    elif digits[0] not in "6789":
        return number, "Invalid - Indian mobile 6/7/8/9 se shuru hota hai"
    return number, "Invalid format"

test_numbers = [
    "9876543210",
    "09876543210",
    "919876543210",
    "98765-43210",
    "+91 98765 43210",
    "12345",
    "5876543210",
]

print("=== Phone Number Formatter ===")
print("\nTest Results:")
print("-" * 55)
for n in test_numbers:
    fmt, status = format_phone(n)
    print(f"  {n:22} -> {fmt:22} [{status}]")

print("\nApna number format karo:")
while True:
    num = input("Number (q=quit): ")
    if num == "q": break
    fmt, status = format_phone(num)
    print(f"  Formatted: {fmt}")
    print(f"  Status   : {status}\n")
OUTPUT
Test Results:
-------------------------------------------------------
  9876543210             -> +91 98765-43210  [Valid Indian Mobile]
  09876543210            -> +91 98765-43210  [Valid (0 prefix)]
  98765-43210            -> +91 98765-43210  [Valid Indian Mobile]
  12345                  -> 12345            [Invalid - bahut chhota!]
  5876543210             -> 5876543210       [Invalid - 6/7/8/9 se shuru hona chahiye]

Code kaise kaam karta hai?

re.sub(r'[^0-9]', '', n) se sirf digits rakhte hain - spaces, dashes, + sab hata dete hain. Multiple formats handle hote hain. Real-world use case!
Advertisement

📋 Project ka Introduction

Phone Number Formatter Python ka Day 59 ka project hai. Is project mein aap Python ke important concepts practice karenge jo real-world applications mein bahut use hote hain.

Yeh project beginners ke liye design kiya gaya hai lekin kaafi concepts cover karta hai. Step by step samjho, code chalao, aur khud modify karke practice karo.

Is tarah ke projects banane se aapka Python confidence badh jaata hai aur aap asli problems solve karna seekh jaate hain. Chaliye code samjhte hain!

🧠 Is Project mein kya seekhoge?

Yeh project banate waqt aap ye Python concepts use karoge:
ConceptKya karta hai
VariablesData store karna
FunctionsReusable code blocks
LoopsRepeat karna
ConditionsDecisions lena
Input/OutputUser se interact karna

📝 Code kaise kaam karta hai — Step by Step

Neeche code ki poori logic step-by-step samjhayi gayi hai:
  1. Problem samjho: Phone Number Formatter mein kya karna hai
  2. Required variables aur data structures decide karo
  3. Logic step-by-step likhó
  4. Code mein implement karo
  5. Test karo aur bugs fix karo

⚠️ Common Mistakes — Bhool mat jaana!

Beginners yeh galtiyan aksar karte hain — dhyan rakho:
  • ⚠️ Indentation sahi rakho — Python mein spaces matter karti hain
  • ⚠️ Variables ko use se pehle define karo
  • ⚠️ Input ko int()/float() mein convert karo agar number chahiye
  • ⚠️ Edge cases handle karo — kya hoga agar user galat input de?

🏋️ Practice Exercises — Aage badho!

Yeh project complete karne ke baad in exercises se practice karo:
  • 💡 Phone Number Formatter mein naya feature add karo
  • 💡 Code ko functions mein refactor karo
  • 💡 Error handling improve karo
  • 💡 File mein data save karo

❓ Aksar Pooche Jane Wale Sawal (FAQ)

Q: Phone Number Formatter project kyon banana chahiye?

A: Har project ek naya concept sikhata hai. Practice se hi Python fluent aati hai.
Q: Code run nahi ho raha?

A: Indentation check karo, syntax errors dekho, variables define hain ya nahi check karo.
Q: Kaise improve karein?

A: Pehle basic version complete karo, phir ek ek feature add karte jao.