Understanding % Operator String Formatting in Python
Python एक शक्तिशाली और लोकप्रिय प्रोग्रामिंग भाषा है, जिसमें डेटा को सही और सुंदर तरीके से प्रदर्शित करना बहुत महत्वपूर्ण होता है। इसके लिए Python में कई प्रकार के String Formatting Methods उपलब्ध हैं। इनमें से एक पुराना लेकिन महत्वपूर्ण तरीका है % Operator String Formatting।
इस लेख में हम निम्न Python code snippet का output विस्तार से समझेंगे:
"%d %s %g you" % (1, 'hello', 4.0)
हम इसे step-by-step और topic-by-topic समझेंगे ताकि beginners और students दोनों आसानी से समझ सकें।
String Formatting क्या है? (What is String Formatting?)
String Formatting का अर्थ है किसी string के अंदर values को insert करना। इसका उपयोग output को readable और structured बनाने के लिए किया जाता है।
उदाहरण:
name = "Ram"print("Hello %s" % name)
Output:
Hello Ram
यहाँ %s की जगह "Ram" insert हुआ।
% Operator क्या है? (What is % Operator in Python?)
Python में % operator का उपयोग string formatting के लिए किया जाता है। इसे Format Operator भी कहा जाता है।
इसमें:
- Left side → format string होती है
- Right side → values होती हैं
Syntax:
"format string" % (values)
Format Specifiers क्या होते हैं? (What are Format Specifiers?)
Format specifiers special symbols होते हैं, जो बताते हैं कि किस प्रकार की value insert करनी है।
मुख्य Format Specifiers:
| Specifier | Meaning | Example |
|---|---|---|
| %d | Integer | 10 |
| %s | String | hello |
| %g | Float / General | 4.0 |
| %f | Float | 4.000000 |
दिया गया Code Snippet (Given Code Snippet)
"%d %s %g you" % (1, 'hello', 4.0)
इसमें:
Format String:
"%d %s %g you"
Values:
(1, 'hello', 4.0)
Step-by-Step Explanation (चरणबद्ध व्याख्या)
Step 1: %d Specifier
%d → Integer value insert करेगा
Value:
1
Result:
1
Step 2: %s Specifier
%s → String value insert करेगा
Value:
hello
Result:
hello
Step 3: %g Specifier
%g → Float value insert करेगा
Value:
4.0
%g unnecessary decimal zeros हटा देता है।
इसलिए:
4.0 → 4
Step 4: Remaining text
Format string में "you" पहले से लिखा हुआ है।
Final Output Formation
अब सभी parts को combine करें:
1 hello 4 you
Final Answer (अंतिम उत्तर)
1 hello 4 you
Python Internally क्या करता है? (What Python Does Internally?)
Python यह steps follow करता है:
- Format string को पढ़ता है
- Specifiers (%d, %s, %g) पहचानता है
- Values को matching specifier के अनुसार replace करता है
- Final string return करता है
Real-Life Example (वास्तविक जीवन उदाहरण)
name = "Amit"age = 20marks = 85.5print("Name: %s Age: %d Marks: %g" % (name, age, marks))
Output:
Name: Amit Age: 20 Marks: 85.5
%g Specifier की विशेषता (Special Feature of %g)
print("%g" % 4.0)
Output:
4
लेकिन:
print("%f" % 4.0)
Output:
4.000000
इसलिए %g cleaner output देता है।
Modern Alternatives (आधुनिक विकल्प)
आजकल Python में नए formatting methods अधिक उपयोग होते हैं:
1. format() method
"{} {} {} you".format(1, "hello", 4.0)
Output:
1 hello 4.0 you
2. f-string (Best Method)
f"{1} {'hello'} {4.0} you"
Output:
1 hello 4.0 you
यह Concept क्यों महत्वपूर्ण है? (Why This Concept is Important?)
यह concept उपयोगी है:
- Python Exams
- Coding Interviews
- Competitive Exams
- Real-world Programming
- Output Formatting
Common Mistakes (सामान्य गलतियाँ)
निष्कर्ष (Conclusion)
Python में % operator string formatting एक महत्वपूर्ण concept है। यह format specifiers जैसे %d, %s, %g का उपयोग करके values को string में insert करता है।
दिए गए code snippet में:
"%d %s %g you" % (1, 'hello', 4.0)
Python values को replace करके final output देता है:
1 hello 4 you
यह concept Python programming और interviews के लिए बहुत महत्वपूर्ण है।
Related MCQ Questions (10 MCQs in Hindi)
1. %d specifier किसके लिए उपयोग होता है?
✅ उत्तर: C
2. %s specifier किसके लिए उपयोग होता है?
✅ उत्तर: C
3. %g specifier किसके लिए उपयोग होता है?
✅ उत्तर: B
4. "%d" % 5 का output क्या होगा?
✅ उत्तर: B
5. "%s" % "hello" का output क्या होगा?
✅ उत्तर: A
6. "%g" % 4.0 का output क्या होगा?
✅ उत्तर: B
7. String formatting के लिए कौन-सा operator उपयोग होता है?
✅ उत्तर: C
8. "%d %s" % (5, "hi") का output क्या होगा?
✅ उत्तर: A
9. % operator किस type पर काम करता है?
✅ उत्तर: B
10. Python में सबसे modern formatting method कौन-सा है?
✅ उत्तर: C

0 Comments