Python String zfill() Last Updated : 02 Jan, 2025 Comments Improve Suggest changes Like Article Like Report zfill() method in Python is used to pad a string with zeros (0) on the left until it reaches a specified width. In this article, we'll see how zfill() method works. Python s = "42" padded_text = s.zfill(5) print(padded_text) Output00042 Explanation:The string "42" is padded with three zeros on the left to make its total length 5.The zfill() method ensures that the string is right-aligned and padded with zeros.Table of ContentSyntax of zfill() methodExamples of String zfill() methodFrequently Asked Questions (FAQs) on zfill()Syntax of zfill() methodstring.zfill(width)Parameterswidth (required): The total length of the string after padding.If the specified width is less than or equal to the length of the original string, no padding is applied.Return TypeReturns a new string with zeros padded on the left to meet the specified width.Examples of String zfill() method1. Padding a shorter stringLet's see how zfill() behaves with a string shorter than the specified width: Python s = "7" padded_text = s.zfill(3) print(padded_text) Output007 Explanation:The original string "7" has a length of 1.The zfill(3) method pads the string with two zeros on the left to make its total length 3.2. String equal to the specified widthWhat happens when the string length matches the specified width? Python s = "12345" padded_text = s.zfill(5) print(padded_text) Output12345 Explanation:The string "12345" already has a length of 5.No padding is added because the string length matches the specified width.3. String longer than the specified widthLet’s see how zfill() handles a string longer than the specified width: Python s = "Python" padded_text = s.zfill(4) print(padded_text) OutputPython Explanation:The string "Python" has a length of 6, which is greater than the specified width of 4.The method returns the original string without any changes.4. Using zfill() with negative and positive numbersWhen dealing with strings representing numbers, the zfill() method correctly handles the sign: Python #positive and negative numbers s1 = "42" s2 = "-42" print(s1.zfill(5)) print(s2.zfill(5)) Output00042 -0042 Explanation:For "42", zeros are added to the left to make its total length 5.For "-42", the minus sign is preserved, and zeros are added after the sign to maintain the width. Comment More infoAdvertise with us Next Article Python String zfill() P pawan_asipu Follow Improve Article Tags : Misc Python python-string Practice Tags : Miscpython Similar Reads Python String replace() Method The replace() method replaces all occurrences of a specified substring in a string and returns a new string without modifying the original string.Letâs look at a simple example of replace() method.Pythons = "Hello World! Hello Python!" # Replace "Hello" with "Hi" s1 = s.replace("Hello", "Hi") print( 2 min read Python String rfind() Method Python String rfind() method returns the rightmost index of the substring if found in the given string. If not found then it returns -1.ExamplePythons = "GeeksForGeeks" print(s.rfind("Geeks"))Output8 Explanationstring "GeeksForGeeks" contains the substring "Geeks" twice.rfind() method starts the sea 4 min read Python String rindex() Method Python String rindex() method returns the highest index of the substring inside the string if the substring is found. Otherwise, it raises ValueError.Let's understand with the help of an example:Pythons = 'geeks for geeks' res= s.rindex('geeks') print(res)Output10 Note: If start and end indexes are 2 min read Python String rpartition() Method String rpartition() method in Python is a powerful tool for splitting a string into three parts, based on the last occurrence of a specified separator. It provides an efficient way to handle text parsing when we want to divide strings from the rightmost delimiter. Let us start with a simple example: 4 min read Python String rsplit() Method Python String rsplit() method returns a list of strings after breaking the given string from the right side by the specified separator. It's similar to the split() method in Python, but the difference is that rsplit() starts splitting from the end of the string rather than from the beginning. Exampl 3 min read Python String rstrip() Method The rstrip() method removes trailing whitespace characters from a string. We can also specify custom characters to remove from end of string.Let's take an example to remove whitespace from the ends of a string.Pythons = "Hello Python! " res = s.rstrip() print(res)OutputHello Python! Syntax of rstrip 2 min read Python String splitlines() method In Python, the splitlines() method is used to break a string into a list of lines based on line breaks. This is helpful when we want to split a long string containing multiple lines into separate lines. The simplest way to use splitlines() is by calling it directly on a string. It will return a list 2 min read Python - String startswith() startswith() method in Python checks whether a given string starts with a specific prefix. It helps in efficiently verifying whether a string begins with a certain substring, which can be useful in various scenarios like:Filtering dataValidating inputSimple pattern matchingLetâs understand by taking 2 min read Python String strip() Method strip() method in Python removes all leading and trailing whitespace by default. You can also specify a set of characters to remove from both ends. It returns a new string and does not modify the original.Let's take an example to remove whitespace from both ends of a string.Pythons = " GeeksforGeeks 2 min read Python string swapcase() Method swapcase() method in Python is used to return a new string where all the uppercase characters in the original string are converted to lowercase, and all the lowercase characters are converted to uppercase. Let's us understand this with the help of an example:Pythons = "I love Python" swapped_text = 2 min read Like