Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

Python में zfill() Method क्या है?


zfill() Method: String में Leading Zeros Add करना

zfill Method String में Left Side Zeros Pad करने के लिए बनाया गया है।

उदाहरण

>>> '25'.zfill(6)
'000025'

Alternative: rjust() Method

हम String Class के rjust(width[, fillchar]) Method का भी उपयोग कर सकते हैं जो String को Right Justify करता है और Left Side को Given Filler Char से Pad करता है। Default Filler Char Space होता है लेकिन हम इसे '0' भी Provide कर सकते हैं।

>>> '25'.rjust(6, '0')
'000025'

String Formatting का उपयोग

हम Python String Formatting का उपयोग करके भी यही Result प्राप्त कर सकते हैं:

>>> print("%06d" % 25)
'000025'

Modern Formatting (Python 3.6+):

>>&>> f"{25:06d}"
'000025'

>>> format(25, '06d')
'000025'

Comparison:

Method Example Output
zfill(6) '25'.zfill(6) '000025'
rjust(6, '0') '25'.rjust(6, '0') '000025'
%06d '%06d' % 25 '000025'
f-string f"{25:06d}" '000025'

Note: zfill Sign को Handle करता है: '-25'.zfill(6) देता है '-00025'।


  1. - Python Bytestring क्या है?

    Python Bytestring एक String Characters का Sequence होता है; ये एक Abstract Concept हैं और इन्हें Directly Disk पर Store नहीं किया जा सकता। एक Byte String Bytes का Sequence होती है - ऐसी चीजें जो Disk पर Store की जा सकती हैं। इनके बीच Mapping को Encoding कहते हैं - इनमें से काफी सारे हैं (और अनंत सं

  1. - Python String की Max Length क्या है?

    Python String की Maximum Length 64-bit Python Installation और 64 GB Memory के साथ, लगभग 63 GB की Python 2 String काफी Feasible होनी चाहिए। यदि आप अपनी Memory को इससे कहीं अधिक Upgrade कर सकते हैं, तो आपकी Maximum Feasible Strings आनुपातिक रूप से Longer होनी चाहिए। लेकिन यह Runtimes पर Hit के साथ आ

  1. - Python में सबसे Efficient String Concatenation Method कौन सा है?

    String Concatenation के Efficient Methods String Variable में String Append करने का Best तरीका + या += का उपयोग करना है। ऐसा इसलिए क्योंकि यह Readable और Fast है। वे उतने ही Fast हैं। इसके अलावा, यदि आप 2 Strings के साथ काम कर रहे हैं, तो append() का भी उपयोग किया जा सकता है। यदि आप Strings की Li