Important Notice:

write() Function (write() Method)

write() Function (write() Method)

4 views 1 min read

write() Function (write() Method) :-

write() Method का उपयोग किसी File में एक String लिखने के लिए किया जाता है। यह दिए गए String के Characters की संख्या Return करता है।

English: The write() method is used to write a string into a file. It returns the number of characters written.

Syntax:

file.write(string)

यहाँ:

  • file – File Object है।

  • write() – Data लिखने का Method है।

  • string – वह Text है जिसे File में लिखना है।

Example 1: String लिखना

file = open("demo.txt", "w", encoding="utf-8")

file.write("Hello Python")

file.close()

File Content:

Hello Python

Example 2: write() का Return Value

file = open("demo.txt", "w", encoding="utf-8")

result = file.write("Hello Python")

print(result)

file.close()

Output:

12

Explanation:

Hello Python में कुल 12 Characters हैं। इसमें एक Space भी शामिल है। इसलिए write() Method 12 Return करता है।

Example 3: अलग-अलग बार write() का उपयोग

file = open("demo.txt", "w", encoding="utf-8")

file.write("Hello")
file.write(" Python")

file.close()

File Content:

Hello Python
 
Explanation:

 

  • पहला write() File में Hello लिखता है।

  • दूसरा write() उसके तुरंत बाद Python लिखता है।

  • write() Automatically New Line नहीं जोड़ता।

Important Points:

  1. write() एक String को File में Write करता है।

  2. यह लिखे गए Characters की संख्या Return करता है।

  3. write() Automatically New Line नहीं जोड़ता।

  4. नई Line के लिए \n का उपयोग किया जाता है।

  5. Text Mode में write() को String (str) देना आवश्यक है।

Related Notes