In the previous article, we have discussed Python String splitlines() Method with Examples
zfill() Method in Python:
The zfill() method appends zeros (0) to the beginning of a string until it reaches the specified length.
If the len parameter value is less than the length of the string, no filling is performed.
Syntax:
string.zfill(len)
Parameters
len: This is Required. A number indicating the string’s desired length.
Return Value:
zfill() returns a copy of the string with 0 to the left filled in. The length of the returned string is determined by the width specified.
- Assume the string’s initial length is 10. Furthermore, the width is specified as 15. zfill() returns a copy of the string with five ‘0’ digits filled to the left in this case.
- Assume the string’s initial length is 10. In addition, the width is specified as 8. In this case, zfill() returns a copy of the original string rather than filling ‘0’ digits to the left. In this case, the length of the returned string will be 10.
Examples:
Example1:
Input:
Given string = "hello btechgeeks" Given number = 10
Output:
hello btechgeeks
Example2:
Input:
Given string = "hello btechgeeks" Given number = 25
Output:
000000000hello btechgeeks
String zfill() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Give the number as static input and store it in another variable.
- Apply zfill() function to the given first string by passing the above-given number as an argument that Fills the string with zeros until it reaches a length of a given number of characters.
- Store it in another variable.
- Print the above result.
- The Exit of Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = "35" # Give the number as static input and store it in another variable. gvn_numb1 = 10 # Apply zfill() function to the given first string by passing the above-given # number as an argument that Fills the string with zeros until it reaches a # length of given number of characters. # Store it in another variable. rslt = gvn_fststr.zfill(gvn_numb1) # Print the above result. print(rslt)
Output:
0000000035
Similarly, do the same for the other strings and print the result.
# Similarly, do the same for the other strings and print the result. gvn_scndstr = "hello btechgeeks" gvn_numb2 = 20 print(gvn_scndstr.zfill(gvn_numb2))
Output:
0000hello btechgeeks
gvn_thrdstr = "20.55" gvn_numb3 = 8 print(gvn_thrdstr.zfill(gvn_numb3))
Output:
00020.55
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Give the number as user input using the int(input()) function and store it in another variable.
- Apply zfill() function to the given first string by passing the above-given number as an argument that Fills the string with zeros until it reaches a length of a given number of characters.
- Store it in another variable.
- Print the above result.
- The Exit of Program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable. gvn_fststr = input("Enter some Random String = ") # Give the number as user input using the int(input()) function and store it in another variable. gvn_numb1 = int(input("Enter some Random number = ")) # Apply zfill() function to the given first string by passing the above-given # number as an argument that Fills the string with zeros until it reaches a # length of given number characters. # Store it in another variable. rslt = gvn_fststr.zfill(gvn_numb1) # Print the above result. print("The result string = ", rslt)
Output:
Enter some Random String = good morning btechgeeks Enter some Random number = 11 The result string = good morning btechgeeks
Similarly, do the same for other strings and print the result.
Below is the implementation:
# Similarly, do the same for other strings and print the result. gvn_scndstr = input("Enter some Random String = ") gvn_numb2 = int(input("Enter some Random number = ")) rslt = gvn_scndstr.zfill(gvn_numb2) print("The result string = ", rslt)
Output:
Enter some Random String = 250.6 Enter some Random number = 6 The result string = 0250.6
Below is the implementation:
# Similarly, do the same for other strings and print the result. gvn_thrdstr = input("Enter some Random String = ") gvn_numb3 = int(input("Enter some Random number = ")) rslt = gvn_thrdstr.zfill(gvn_numb3) print("The result string = ", rslt)
Output:
Enter some Random String = welcome to btechgeeks Enter some Random number = 27 The result string = 000000welcome to btechgeeks
Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.