← Back to Swift Course | Chapter 1: Setup & Basics | Lesson 5 of 7

print() Function की जानकारी

print() वह command है जिसे आप उपयोग करते हैं computer को screen पर text या numbers दिखाने के लिए ताकि आप देख सकें आपका program क्या कर रहा है।
Syntax
swift
print(value)
print("text \(expression)")              // interpolation
print(value1, value2, separator: " ")

Basic Printing

print का सबसे simple उपयोग एक single string लेता है और इसे console में लिखता है, आखिर में अपने आप एक newline character जोड़ते हुए ताकि अगली print call एक नई line पर शुरू हो।

उदाहरण: Basic Printing

markup
print("First line")
print("Second line")

String Interpolation

Swift आपको \(expression) उपयोग करके variables और expressions को सीधे किसी string के अंदर embed करने देती है, जो + के साथ strings को concatenate करने से कहीं ज़्यादा साफ़ है।

Note: आप parentheses के अंदर कोई भी expression डाल सकते हैं, सिर्फ एक variable नहीं, जैसे \(2 + 2)।

उदाहरण: String Interpolation

markup
// Declare a constant `name`, set to `"Swift"`
let name = "Swift"
// Declare a constant `version`, set to `5.9`
let version = 5.9
// Print "Learning \(name) version \(version)" to the console
print("Learning \(name) version \(version)")

Multiple Arguments and separator

print कई comma-separated values accept करता है और default रूप से इन्हें एक space से जोड़ता है। separator parameter customize करता है कि इनके बीच क्या दिखे।

उदाहरण: Multiple Arguments and separator

markup
print("apple", "banana", "cherry")
print("apple", "banana", "cherry", separator: " - ")

Custom terminator

Default रूप से print एक newline से खत्म होता है, लेकिन terminator parameter इसे किसी और चीज़ से replace कर सकता है, जैसे एक empty string ताकि उसी line पर print करते रहें।

Note: terminator: "" उपयोग करना कई print calls में एक single line of output बनाने के लिए handy है।

उदाहरण: Custom terminator

markup
// Print "Loading", terminator: "" to the console
print("Loading", terminator: "")
// Print "...", terminator: "" to the console
print("...", terminator: "")
// Print "done" to the console
print("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 हर call के बाद अपने आप एक newline जोड़ता है, अनपेक्षित रूप से stacked output की ओर ले जाते हुए।
  2. string interpolation \() उपयोग करने के बजाय एक number को सीधे + से किसी string में concatenate करने की कोशिश करना।
  3. separator और terminator parameters के बारे में न जानना, फिर manually spaces या newlines insert करना जो print पहले से handle करता है।
चैप्टर सारांश
  • print() text को standard output में लिखता है, default रूप से एक newline के बाद।
  • \(value) से String interpolation variables और expressions को सीधे किसी string के अंदर embed करता है।
  • separator parameter control करता है कि कई printed items के बीच क्या जाए, और terminator control करता है कि बाद में क्या आए।
  • print() कई comma-separated arguments ले सकता है, default रूप से उनके बीच एक space के साथ printed।
🔒

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.