Print Statement’s Example

Print Statement’s Example – print() Function के इन विभिन्‍न Parameters को ठीक से समझने के लिए हम निम्‍नानुसार एक Example Program Create कर सकते हैं-

[code]
FileName: PrintStatements.py
# print() Function Call for Newly Line Only
print()
name = "KRISHNA"
age = 6
studyClass = "2nd"
subs = ['Hindi','English','Maths']
# print() Function Call with Default Separator
print(name, age, studyClass, subs)
# print() Function Call without any Separator
print(name, age, studyClass, subs, sep='')
# print() Function Call with Comma Separator
print(name, age, studyClass, subs, sep=", ")
print()
# print() Function Call with Default Endline
print(name, age, studyClass); print(subs)
# print() Function Call without any Endline
print(name, age, studyClass, end=''); print(subs)
# print() Function Call with Comma Separator
print(name, age, studyClass, subs, end="...\n")
print()
# Open file data.txt and Print Content into it.
print(name, age, studyClass, subs, sep=' | ',file=open('data.txt','w'))
# Open file data.txt, Read it's Content and Print to stdout.
print(open('data.txt','r').read())

Output
KRISHNA 6 2nd ['Hindi', 'English', 'Maths']
KRISHNA62nd['Hindi', 'English', 'Maths']
KRISHNA, 6, 2nd, ['Hindi', 'English', 'Maths']
KRISHNA 6 2nd
['Hindi', 'English', 'Maths']
KRISHNA 6 2nd['Hindi', 'English', 'Maths']
KRISHNA 6 2nd ['Hindi', 'English', 'Maths']...
KRISHNA | 6 | 2nd | ['Hindi', 'English', 'Maths']
[/code]

print() Function मूलत: sys.stdout Object के लिए एक Simple Interface Provide करता है, जिसमें मामूली सा Default Formatting होता है। इसलिए वास्‍तव में जब हम print() Function को Use करते हैं, तो केवल print() Statement लिखने के स्‍थान पर हम इसे sys.stdout.write() भी लिख सकते हैं।

यानी print() Function वास्‍तव में इसी sys.stdout.write() का छोटा रूप है, जो स्‍वयं ही अपने स्‍तर पर थोड़ी-बहुत Formatting भी कर लेता है। उदाहरण के लिए जब हम निम्‍न print() Statement लिखते हैं-

   print(X + Y)

तब Python Internally इसी print() Statement को निम्‍नानुसार Expand करता है-

import sys

sys.stdout.write(str(X) + ‘ ‘ + str(Y) + ‘\n’)

यानी print() Function Internally str() Function को Call करके Parameters को Equivalent String Objects में Translate करता है और फिर दोनों String Objects के बीच एक Blank Space Character Add करता है और उसके बाद अन्‍त में एक Newline Character Constant Append करके Output को Screen पर Display कर देता है। जबकि यदि हम print() Function के स्‍थान पर sys.stdout.write() Method को Use करें, तो ये सभी काम हमें स्‍वयं अपने स्‍तर पर Manually करने पड़ते हैं।

हालांकि Default रूप से print() Function सारे Content को Standard Output Stream Device पर ही भेजता है। फिर भी कभी-कभी ऐसी जरूरत होती है, जब हम हमारे Content को Standard Output Stream के बजाय किसी अन्‍य Stream जैसे कि एक error.log File पर Send करना चाहते हैं, ताकि बाद में जरूरत पड़ने पर उस Log File के Content को Check कर सकें।

इस तरह का Redirection, Python Shell के माध्‍यम से भी किया जा सकता है। और यदि हम चाहें तो इस तरह की जरूरत को पूरा करने के लिए हम Python के Print Stream Redirection Concept को भी Use करते हैं। जैसे-

import sys

sys.stdout = open(‘log.txt’, ‘a’) # Redirects Prints to a log.txt File

जब ये Statement Execute होता है, तब Current Working Directory में Automatically log.txt नाम का एक File Append Mode में Create हो जाता है और जो भी Content print() Function के द्वारा Print किया जाता है, वो सारा Content इस log.txt File में Redirect होकर Write होने लगता है, जब तक कि हम फिर से sys.stdout को Manually Reset नहीं कर देते।

हम जब चाहें तब इस sys.stdout Object को किसी भी Object Type से Set कर सकते हैं और जहां कहीं पर भी हम इसे Set करते हैं, उसके बाद के सभी print() Functions द्वारा Specify किए गए सभी Content उसी Specified Object में  Print होने लगते हैं, जिन्‍हें Print करने का काम इस sys.stdout का write() Method करता है।

Output Stream को इस तरह से Reset करके हम कई Special तरह की Print से सम्‍बंधित जरूरतों को पूरा कर सकते हैं, जिन्‍हें print() Function द्वारा उतने बेहतर तरीके से नहीं किया जा सकता।

उदाहरण के लिए यदि हमें पता है कि हमारे Content को हमें किसी File Object में ही Write करना है, तो हम इस तरह से sys.stdout Object को Set करके अपने सारे Content को write() Method द्वारा File Object में आसानी से Redirect कर सकते हैं। Redirection की इस पूरी प्रक्रिया को निम्‍न Example द्वारा काफी आसानी से समझा जा सकता है-

[code]
FileName: PrintRedirect.py
import sys
# Save sys.stdout's Default Reference for Restoring Later
temp = sys.stdout
# Redirect print() Function's Content to a File log.txt
sys.stdout = open('log.txt', 'a')
# All Prints goes to File Object's Buffer from here
print('Name: KRISHNA')
print('Age: 6')
print('Class: 2nd')
# Flush Buffered Output to External Disk
sys.stdout.close()
# Restore sys.stdout to it's Default Stream from here
sys.stdout = temp
# Prints will Show-up Again on Screen Now
print('Content of the log.txt File')
# Content Printed in File using print() Function
print(open('log.txt').read())

Output
Content of the log.txt File
Name: KRISHNA
Age: 6
Class: 2nd
[/code]

हालांकि इस तरह से sys.stdout Object के माध्‍यम से Text Content को Specified Object में Print करना काफी उपयोगी तरीका है, लेकिन इस तरीके का एक नुकसान भी है और नुकसान यही है कि हम इस sys.stdout Object को एक बार Set करने के बाद फिर से उसे Manually Reset करना जरूरी होता है और कोई ऐसा Built-In तरीका नहीं है, कि हमारा काम पूरा होने के बाद ये Automatically फिर से Default Output Stream के Reference से Reset हो जाए।

इसीलिए इस Example में हमने सबसे पहले sys.stdout Object का Reference, temp नाम के Variable में Save किया है और File में Printing का काम समाप्‍त होने के के बाद इसे फिर से temp में Stored Original Reference से Reset कर दिया है, ताकि फिर से print() Function में Specify किया गया Content, Output Screen पर Show होने लगे।

इसीलिए Python 3.x में print() Function के अन्‍दर ही file नाम के Parameter को Specify करने की सुविधा Provide किया गया है, ताकि जैसे ही Current print() Function, Content को file Object में Write कर दे उसके बाद Automatically sys.stdout Object अपने Original Output Stream से Reset हो जाए।

Python in Hindi - BccFalna.comये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Python in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी है, तो निश्चित रूप से ये EBook भी आपके लिए काफी उपयोगी साबित होगी।

Python in Hindi | Page: 602 | Format: PDF

BUY NOW