2012-08-24 14 views
44

मैंने पढ़ा है कि प्रिंट स्टेटमेंट के बाद नई लाइन को दबाने के लिए आप टेक्स्ट के बाद कॉमा डाल सकते हैं। उदाहरण here पाइथन 2 की तरह दिखता है। यह पायथन 3 में कैसे किया जा सकता है?प्रिंट स्टेटमेंट के बाद मैं नई लाइन को कैसे दबा सकता हूं?

उदाहरण के लिए:

for item in [1,2,3,4]: 
    print(item, " ") 

क्या इतना बदलने के लिए है कि यह उन्हें एक ही लाइन पर प्रिंट की जरूरत है?

+1

तुम सिर्फ कर सकता है 'प्रिंट ('' [में [1, 2, 3, 4] मैं के लिए str (i)]) .join() (' –

+0

'प्रिंट * [1, 2, 3, 4]) 'एक अंतरिक्ष अलग अनुक्रम मुद्रण के सामान्य मामले के लिए काम करता है –

उत्तर

82

प्रश्न पूछता: "कैसे यह अजगर में किया जा सकता 3?"

उपयोग इस अजगर 3.x के साथ निर्माण:

for item in [1,2,3,4]: 
    print(item, " ", end="") 

यह उत्पन्न करेगा:

1 2 3 4 

अधिक जानकारी के लिए यह Python doc देखें:

Old: print x,   # Trailing comma suppresses newline 
New: print(x, end=" ") # Appends a space instead of a newline 

-

एक तरफ:

अलावा, print() समारोह भी sep पैरामीटर एक निर्दिष्ट कैसे अलग-अलग आइटम मुद्रित करने के लिए अलग किया जाना चाहिए देता है कि प्रदान करता है। उदा।

In [21]: print('this','is', 'a', 'test') # default single space between items 
this is a test 

In [22]: print('this','is', 'a', 'test', sep="") # no spaces between items 
thisisatest 

In [22]: print('this','is', 'a', 'test', sep="--*--") # user specified separation 
this--*--is--*--a--*--test 
+1

बिल्कुल सही! तो अंत = "" सिर्फ न्यूलाइन चरित्र को ओवरराइड करता है। – Ci3

+0

@ChrisHarris दस्तावेज़ों से उद्धरण के साथ अद्यतन देखें, इसलिए आप '' '(खाली स्ट्रिंग) – Levon

+1

के साथ नई लाइन को बदल रहे हैं बहुत उपयोगी, धन्यवाद! – Ci3

4

प्रिंट पायथन 3.0 तक कथन से फ़ंक्शन में संक्रमण नहीं हुआ। तुम बड़े अजगर का उपयोग कर रहे हैं तो आप तो जैसे एक अनुगामी अल्पविराम के साथ न्यू लाइन दबाने कर सकते हैं:

print "Foo %10s bar" % baz, 
+4

को इंगित करने में मदद के लिए धन्यवाद, प्रश्न विशेष रूप से पायथन 3 का उपयोग करने के बारे में पूछा गया। –

0

अजगर 3 प्रिंट() फ़ंक्शन की अनुमति देता है क्योंकि अंत = "" परिभाषा है कि मुद्दों के बहुमत संतुष्ट करता है।

मेरे मामले में, मैं प्रीटीप्रिंट चाहता था और निराश था कि यह मॉड्यूल समान रूप से अपडेट नहीं किया गया था। तो मैं इसे मैं क्या चाहता था कर दिया:

from pprint import PrettyPrinter 

class CommaEndingPrettyPrinter(PrettyPrinter): 
    def pprint(self, object): 
     self._format(object, self._stream, 0, 0, {}, 0) 
     # this is where to tell it what you want instead of the default "\n" 
     self._stream.write(",\n") 

def comma_ending_prettyprint(object, stream=None, indent=1, width=80, depth=None): 
    """Pretty-print a Python object to a stream [default is sys.stdout] with a comma at the end.""" 
    printer = CommaEndingPrettyPrinter(
     stream=stream, indent=indent, width=width, depth=depth) 
    printer.pprint(object) 

अब, जब मैं करता हूँ:

comma_ending_prettyprint(row, stream=outfile) 

मैं क्या मैं चाहता था मिल (स्थानापन्न आप क्या चाहते हैं - अपने माइलेज वेरी)

3

अजगर 3.6.1

print("This first text and " , end="") 

print("second text will be on the same line") 

print("Unlike this text which will be on a newline") 

आउटपुट के लिए कोड

>>> 
This first text and second text will be on the same line 
Unlike this text which will be on a newline 
+0

यह स्पष्ट अंत = "" पैरामीटर अगली पंक्ति को कैसे प्रभावित करेगा, लेकिन उसके बाद कोई नहीं - धन्यवाद! –

संबंधित मुद्दे

 संबंधित मुद्दे