← Back to Kotlin Course | Chapter 1: Setup & Basics | Lesson 6 of 7

print() और println() में अंतर

print() और println() screen पर text दिखाने के दो तरीके हैं; एक cursor को text के ठीक बाद छोड़ता है, और दूसरा इसे नई line पर ले जाता है।
Syntax
kotlin
print(value)      // no newline
println(value)    // adds a newline

Using print()

print() बाद में newline जोड़े बिना standard output पर text लिखता है, इसलिए अगली चीज़ जो print होती है वह ठीक वहीं जारी रहती है जहां पिछला output रुका था।

उदाहरण: Using print()

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "Hello, " to the console
    print("Hello, ")
    // Print "World!" to the console
    print("World!")
    // Print " No newline between these." to the console
    print(" No newline between these.")
}

Using println()

println() print() जैसा व्यवहार करता है लेकिन आखिर में एक newline character जोड़ देता है, इसलिए हर call output की अगली line पर ताज़ा शुरू होती है।

उदाहरण: Using println()

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "First line" to the console, with a trailing newline
    println("First line")
    // Print "Second line" to the console, with a trailing newline
    println("Second line")
    // Print "Third line" to the console, with a trailing newline
    println("Third line")
}

Printing Non-String Values

print() और println() दोनों numbers, booleans, और दूसरे types सीधे accept करते हैं -- Kotlin print करने से पहले उन्हें अपने आप उनके string representation में convert कर देता है।

उदाहरण: Printing Non-String Values

markup
// Entry point: execution of the program starts here
fun main() {
    // Print `42` to the console, with a trailing newline
    println(42)
    // Print `3.14` to the console, with a trailing newline
    println(3.14)
    // Print `true` to the console, with a trailing newline
    println(true)
}

Mixing print() and println()

दोनों को combine करना आपको print() से टुकड़े-टुकड़े में एक single line बनाने और फिर एक println() call से इसे खत्म करने देता है, जो कई हिस्सों वाले output को format करने के लिए उपयोगी है।

Note: multi-part lines के लिए print() का ज़्यादा उपयोग भ्रामक हो सकता है -- string templates (बाद में कवर किया गया) अक्सर ज़्यादा साफ़ होते हैं।

उदाहरण: Mixing print() and println()

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "Score: " to the console
    print("Score: ")
    // Print `95` to the console
    print(95)
    // Print "/100" to the console, with a trailing newline
    println("/100")
    // Print "Done!" to the console, with a trailing newline
    println("Done!")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. print() को बार-बार उपयोग करना और यह देखकर हैरान होना कि output बिना spaces या newlines के एक line में मिल जाता है।
  2. कई print() calls को साथ chain करते समय manually space या separator जोड़ना भूल जाना।
  3. यह मान लेना कि बिना arguments के println() कुछ नहीं करता, जबकि यह असल में सिर्फ एक newline print करता है।
चैप्टर सारांश
  • print() text को बिना trailing newline के output करता है; अगला output उसी line पर जारी रहता है।
  • println() text को newline के साथ output करता है, बाद के output को अगली line पर ले जाते हुए।
  • दोनों functions numbers, booleans, और दूसरे types को उनके string form में अपने आप convert करके print कर सकते हैं।
  • बिना arguments के call किया गया println() बस एक खाली line print करता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.