Triple Quotes Code Multiline Block Strings

अभी तक हमने Single Quote, Double Quote, Escape Sequence Character Constants व Raw Strings को समझा। इनके अलावा Python में Triple Quotes String Format भी होता है, जिसे Block String के नाम से भी जाना जाता है और इसका प्रयोग Multiple Line Text Data को लिखने के लिए किया जाता है।

Triple Quotes Specify करने के बाद हम जितने चाहें उतने Lines का Text Content लिख सकते हैं और उस Text Content को किसी Variable में Assign कर सकते हैं।

[code]
FileName: TripleQuotes.py
multilineText = """
This is the text
that is being written in multiple lines,
and it will not create any issue.
Generally this behaves like <pre> Element
of the HTML and displays everything as written
between the Triple Quotes.
"""
print(multilineText)

Output
This is the text
that is being written in multiple lines,
and it will not create any issue.
Generally this behaves like <pre> Element
of the HTML and displays everything as written
between the Triple Quotes.
[/code]

Triple Quotes के रूप में हम Double Quotes का भी प्रयोग कर सकते हैं और Single Quotes का भी प्रयोग कर सकते हैं। Triple Quotes Block String का फायदा ये है कि हम इसे HTML के <pre> Element की तरह Use कर सकते हैं। यानी Triple Quotes के बीच हम जो भी कुछ लिखते हैं और जैसा भी लिखते हैं, Output के रूप में हमें वो सबकुछ Exactly वैसा ही दिखाई देता है।

यहां तक कि इस Triple Quotes के बीच यदि हम कोई Python Program Code या Comment भी लिख दें, तो वह Code या Comment भी हमें Output में Exactly उसी तरह से दिखाई देता है जिस तरह से अन्‍य Text Content दिखाई देते हैं।

सामान्‍यत: Professional Development के दौरान Triple Quotes का प्रयोग तब किया जाता है, जब हमें Multiline Error Message, HTML, XML या JSON Code को अपने Python Source Code File में Embed करना होता है। हम चाहें तो इस तरह के Source Codes को Directly भी अपनी Script File में Embed कर सकते हैं या फिर हम इन्‍हें Triple Quotes के माध्‍यम से Declare करके भी Use कर सकते हैं।

इसके अलावा कई बार ऐसी स्थिति होती है जब हम हमारे Python Program के किसी Bunch of Codes को Disable करते हुए Program को Test and Debug करना चाहते हैं। उस स्थिति में भी हम हमारे Program के उन Bunch of Codes को Triple Quotes के माध्‍यम से Disable करते हैं। हालांकि ये कोई Best Practice नहीं है और इसका Python Program की Performance पर काफी Negative Effect पड़ता है। इसलिए Code Disable करने के लिए हमेंशा Comments का ही प्रयोग करना चाहिए।

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

Python in Hindi | Page: 602 | Format: PDF

BUY NOW GET DEMO REVIEWS