Printing Statements in Python

Printing Statements in Python

The print () function prints the given object to the standard output device (screen) or to the text stream file (From Python 3.x version onward). The print function is called a built-in function because it is part of the Python standard functions library. You don’t need to do anything special to get this function. It is provided for your use when you install Python.

print () फंक्शन दिए गए ऑब्जेक्ट को मानक आउटपुट डिवाइस (स्क्रीन) या टेक्स्ट स्ट्रीम फाइल (पायथन 3.x वर्जन से आगे) पर प्रिंट करता है। प्रिंट फंक्शन को एक अंतर्निहित फंक्शन कहा जाता है क्योंकि यह पायथन मानक फंक्शन लाइब्रेरी का हिस्सा है। इस फंक्शन को प्राप्त करने के लिए आपको कुछ विशेष करने की आवश्यकता नहीं है। यह आपके उपयोग के लिए प्रदान किया जाता है जब आप पायथन स्थापित करते हैं।

Syntax:

print(*objects, sep=”, end=’\n’, file=sys.stdout, flush=False)

  • objects – object to the printed. * indicates that there may be more than one object

ऑब्जेक्ट- प्रिंट करने के लिए ऑब्जेक्ट । * इंगित करता है कि एक से अधिक ऑब्जेक्ट हो सकते हैं।

  • sep – objects are separated by sep. Default value:

सेप- ऑब्जेक्ट्स को सेप द्वारा अलग किया जाता है। डिफॉल्ट मानः

  • end – end is printed at last.

इंड- इंड छोर पर मुद्रित है।

  • file – must be an object with write (string) method. If omitted it, sys. stdout will be used which prints objects on the screen.

फाइल- राइट (स्ट्रिंग) मेथड के साथ एक ऑब्जेक्ट होना चाहिए।

  • flush – If True, the stream is forcibly flushed. Default value: False

फ्लश- यदि टू है, तो स्ट्रीम जबरन भर दी जाती है। डिफॉल्ट वैल्यू: फाल्स

Form 1: print () without argument, prints a newline.

print () बिना तर्क के, एक नई लाईन प्रिंट करना।

Example:

>>> print() 

Form 2: To display characters (also called string literals) using the print function, you need to enclose the characters in either a set of single quotes (”) or double quotes (” “) or triple single quotes (” ”’) or triple double quotes (“” “””)

प्रिंट फंक्शन का उपयोग करके कैरेक्टर (जिसे स्ट्रिंग लिटरल भी कहा जाता है) प्रदर्शित करने के लिए, आपको single quotes (‘) या double quotes (” “) या triple single quotes (” ”’) या triple double quotes (“”””””) के अक्षरों को संलग्न करने की आवश्यकता है।

For example:

>>> print ('hello python') 
hello python 
>>> print ('welcome 
SyntaxError: EOL while scanning string literal 
>>> print ("hello python") 
Hello python 
>>> print ("Welcome 
SyntaxError: EOL while scanning string literal

Sometimes you need to output a character string that contains a single quote. In such a case, you should use double quotes around the print function argument.

कभी-कभी आपको एक कैरेक्टर स्ट्रिंग को आउटपुट करने की आवश्यकता होती है जिसमें एक ही कोट होता है। ऐसे मामले में, आपको प्रिंट फंक्शन तर्क के आसपास दोहरे कोट्स का उपयोग करना चाहिए।

For example:

>>>print ("The string object 'Pretty cool, huh!'") 
The string object 'Pretty cool, huh!' 
>>>print ('hello 'python', welcome') 
SyntaxError: invalid syntax 
>>> print ("hello "python", welcome") 
SyntaxError: invalid syntax.

Sometimes, you need to output a string of characters that contain double quotes, such as for a quotation.

कभी-कभी आपको कैरेक्टर के एक स्ट्रिंग को आउटपुट करने की आवश्यकता होती है जिसमें दोहरे कोट्स शामिल होते हैं, जैसे कि एक कोटेशन।

For example:

>>>print(' Quote of the Day - "You create your own reality" - Buddha') 
Quote of the Day - "You create your own reality"  Buddha

Another way to format output using the print function is via triple quotes. Triple quotes are simply three sets of single quotes (” ”) or three sets of double quotes (“”” “””).

प्रिंट फंक्शन का उपयोग करके आउटपुट स्वरूपित करने का दूसरा तरीका ट्रिपल कोट्स के माध्यम से है। ट्रिपल कोट्स केवल एकल कोट्स (” ”) के तीन सेट या दोहरे कोट्स (“”” “‘”) के तीन सेट हैं।

Below example shows how to use triple quotes to embed a linefeed character (via pressing the Enter key). When the output is displayed, each embedded linefeed character causes the next sentence to appear on the next line. Thus, the linefeed moves your output to the next new line.

नीचे दिए गए उदाहरण से पता चलता है कि एक लाइनफीड कैरेक्टर (एंटर की दबाकर) को एम्बेड करने के लिए ट्रिपल कोट्स का उपयोग कैसे करें। जब आउटपुट प्रदर्शित होता है, तो प्रत्येक एम्बेडेड लाइनफीड कैरेक्टर अगले वाक्य को अगली पंक्ति पर दिखाई देता है। इस प्रकार, लाइनफूड आपके आउटपुट को अगली नई लाइन में ले जाता है।

>>> print (' ' ' welcome to python programming "')
welcome to python programming "
>>> print ("""welcome to python programming """)
welcometo python programming

Join lines: In Python, with the help of back slash character we can join lines.

ज्वाइन लाईनः पायथन में, बैक स्लैश कैरेक्टर की मदद से हम लाइनों में शामिल हो सकते हैं।

>>>print('hello  \
word')
hello world 
>>> print ("hello \ 
 world")
hello world 

Escape Sequences – इस्केप सिक्वेन्स

Up to now we learned in string, how to take single quotes or double quotes as a normal string. But sometimes, you need to interpret a sequence of characters within a string in a different way.

अब तक हम स्ट्रिंग में सीखे हैं कि कैसे एक सामान्य स्ट्रिंग के रूप में सिंगल कोट्स या डबल कोट्स लें। लेकिन कभी-कभी, आपको एक स्ट्रिंग के भीतर एक अलग तरीके से कैरेक्टर के अनुक्रम की व्याख्या करने की आवश्यकता होती है।

In this situation, you can achieve this using a backslash (\) character. In Python, a backslash character in a string indicates that one or more characters that follow it should be treated specially. This is called as an escape sequence.

इस स्थिति में, आप बैकस्लैश (1) कैरेक्टर का उपयोग करके इसे प्राप्त कर सकते हैं। पायथन में, एक स्ट्रिंग में एक बैकस्लैश कैरेक्टर इंगित करता है कि एक या अधिक कैरेक्टर जो इसका पालन करते हैं, उन्हें विशेष रूप से व्यवहार किया जाना चाहिए। इसे इस्केप सिक्वेन्स कहा जाता है।

Let’s see how this works. Already we have seen the problems, when you try to include quote characters in a string. If a string is enclosed by single quotes, you can’t directly specify a single quote character as part of the string because, for that string, the single quote has special meaning-it terminates the string.

आइए देखें कि यह कैसे काम करता है। पहले से ही हमने समस्याओं को देखा है, जब आप एक स्ट्रिंग में कोट्स कैरेक्टर को शामिल करने का प्रयास करते हैं। यदि एक स्ट्रिंग सिंगल कोट्स द्वारा संलग्न है, तो आप सीधे स्ट्रिंग के भाग के रूप में एक सिंगल कोट कैरेक्टर निर्दिष्ट नहीं कर सकते, क्योंकि उस स्ट्रिंग के लिए, सिंगल कोट्स का विशेष अर्थ है – यह स्ट्रिंग को समाप्त करता है।

For example:

>>>print('hello  \
word')
hello world 
>>> print ("hello \ 
 world")
hello world 

Specifying a backslash in front of the quote character in a string “escapes” it and causes Python to suppress its usual special meaning. It is then interpreted simply as a literal single quote character.

एक स्ट्रिंग “इस्केप’ में कोट कैरेक्टर के सामने एक बैकस्लैश निर्दिष्ट करना इसके सामान्य अर्थ को दबाने के लिए पायथन का कारण बनता है। यह बस एक लिटरल सिंगल कोट्स कैरेक्टर के रूप में इंटरप्रेट करता है।

For example:

>>> print ('A single quote (\') character.') 
A single quote (') character.

The same works in a string delimited by double quotes as well.

एक स्ट्रिंग में भी दोहरे कोट्स द्वारा डिलिमिटेड एक ही काम करता है।

>>> print ("A double quote (\") character.") 
A double quote ("),character.

The following is a table of escape sequences:
निम्नलिखित इस्केप सिक्वेन्स की एक तालिका है:

\a              Bell
\n		New Line		
\t		Horizontal Tab
\r		Carriage Return
\b		Back Space
\f		Form Feed
\v		Vertical Tab
\’		Single Quote
\”		Double Quote
\\		Back Slash Single
\uxxxx 	Unicode character with 16-bit hex value xxx
\Uxxxxxxxx	Unicode character with 32-bit hex value xxxxxxxx
\oxx 		Character with octal value xx
\xhh		Character with hex value hh

Ordinarily, a newline character terminates line input. So pressing Enter in the middle of a string will cause Python to think it is incomplete.

आमतौर पर, एक नई लाइन कैरेक्टर लाइन इनपुट को समाप्त करता है। तो एक स्ट्रिंग के बीच में एंटर दबाकर पायथन को अधूरा समझेंगे।

For example:

>>> print ('a 
SyntaxError: EOL while scanning string literal

To break up a string over more than one line, include a backslash before each newline, and the newlines will be ignored.

एक स्ट्रिंग को एक से अधिक पंक्ति में तोड़ने के लिए, प्रत्येक नई पंक्ति से पहले एक बैकस्लैश शामिल करें, और नए लाईन को इग्नोर कर दिया जाएगा।

For example:

>>>
>>> print ('a\
b\
c') 
abc

To include a literal backslash in a string, escape it with a backslash.

एक स्ट्रिंग में लिटरल बैकस्लैश शामिल करने के लिए, इसे बैकस्लैश के साथ छोड़ें।

For example:

>
>>> print('hello\\world')
Hello\world

Applying Special Meaning to Characters

कैरेक्टर को विशेष अर्थ देना

For example

>>> print ("hello\tworld") 
Hello	world 
>>> print("a\141\x61")
 aaa 
>>> print("new\nline")
new 
line
print('\u2192 \N{rightwards arrow}')
→→

This type of escape sequence is typically used to insert characters that are not readily generated from the keyboard or are not easily readable or printable.

इस प्रकार का एस्केप सीक्वेंस आमतौर पर उन अक्षरों को डालने के लिए उपयोग किया जाता है जो कीबोर्ड से आसानी से उत्पन्न नहीं होते हैं या आसानी से पढ़ने योग्य या प्रिंट करने योग्य नहीं होते हैं।

Using ‘sep’ parameter in print () function
प्रिंट () फंक्शन में ‘sep’ पैरामीटर का उपयोग करना

In Python 3 print () function can accept multiple arguments to print. The sep parameter defines the separator between the arguments or items in an iterator to print. In Python 3, sep defaults to “space” which is known as “soft spacing”. It is found only in python 3.x or later versions. It is also used for formatting the output strings.

पायथन 3 में print() फंक्शन में प्रिंट करने के लिए कई तर्क स्वीकार कर सकते हैं। सेप पैरामीटर प्रिंट करने के लिए पुनरावृत्तियों या आइटम्स के बीच विभाजक को परिभाषित करता है। पायथन 3 में, सीप डिफॉल्ट को “स्पेस’ करता है जिसे “सॉफ्ट स्पेसिंग” के रूप में जाना जाता है। यह केवल पायथन 3.x या बाद के वर्जन में पाया जाता है। इसका उपयोग आउटपुट स्ट्रिंग्स को फार्मेटिंग करने के लिए भी किया जाता है।

For example:

>>> print (1234) 
1234
>>> print (1,2,3,4) 
1 2 3 4 
>>> print ('1', '2', '3', '4' )
1 2 3 4
>>> print ( '1', '2', '3', '4', sep="* * * ") 
1***2***3***4 
>>> print ('1', '2', '3', '4', sep="#" )
1#2#3#4 
>>>print ("hello",'python',"welcome","to","programm")
hello python welcome to program

Using ‘end’ parameter in print () function 
print ()फंक्शन में “एंड’ पैरामीटर का उपयोग करना

Python’s print () function comes with a parameter called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print statement with any character or string using this parameter.

पायथन का print()फंक्शन एंड नामक पैरामीटर के साथ आता है। डिफॉल्ट रूप से, इस पैरामीटर का मान, ‘\n’ है, यानी नई लाईन कैरेक्टर। आप इस पैरामीटर का उपयोग करके किसी भी कैरेक्टर या स्ट्रिंग के साथ एक प्रिंट स्टेटमेंट को समाप्त कर सकते हैं।

>>> print ("hello programmer", end="welcome") 
hello programmerwelcome
>>> print ("hello programmers !!!", end="\n\n\n") 
hello programmers!!!
>>> print ("hello programmer", end="\n# \n#\n")
hello programmer
#
#

Raw Strings – रॉ स्ट्रिंग

If a string literal preceded by small ‘r or capital ‘R’ that type of string is called Raw String. Raw String specifies that escape sequences in the connected string are not translated.

यदि एक स्ट्रिंग लिटरल छोटे ‘r’ या कैपिटल ‘R’ से पहले होता है तो उस प्रकार के स्ट्रिंग को रॉ स्ट्रिंग कहा जाता है। रॉ स्ट्रिंग निर्दिष्ट करती है कि कनेक्टेड स्ट्रिंग में एस्केप सीक्वेंस अनुवादित नहीं हैं।

For example:

>>> print ('hello\nworld') 
Hello 
world 
>>> print (r'hello\nworld') 
hello\nworld 
>>>print ('I am \\back')
 I am \back 
>>> print (R'be \\happy') 
be \\happy

Chapter wise Model Paper Link

About Me