44
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi đã tự hỏi nếu ai đó có thể giúp tôi thêm số 0 hàng đầu vào chuỗi hiện có này khi các chữ số hát [ví dụ 1-9]. Đây là chuỗi:
str[int[length]/1440/60]
Thefourtheye
226K52 Huy hiệu vàng443 Huy hiệu bạc486 Huy hiệu Đồng52 gold badges443 silver badges486 bronze badges
Đã hỏi ngày 7 tháng 2 năm 2014 lúc 6:15Feb 7, 2014 at 6:15
3
Bạn có thể sử dụng phương pháp str.zfill
tích hợp, như thế này
my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
Từ các tài liệu,
Trả về chuỗi số bên trái chứa đầy số 0 trong một chuỗi chiều rộng. Một tiền tố dấu hiệu được xử lý chính xác. Chuỗi ban đầu được trả về nếu chiều rộng nhỏ hơn hoặc bằng Len [s].
Vì vậy, nếu chiều dài của chuỗi thực tế nhiều hơn chiều rộng được chỉ định [tham số được chuyển cho zfill
], chuỗi được trả về như hiện tại.
Đã trả lời ngày 7 tháng 2 năm 2014 lúc 6:16Feb 7, 2014 at 6:16
Thefourtheyethefourtheyethefourtheye
226K52 Huy hiệu vàng443 Huy hiệu bạc486 Huy hiệu Đồng52 gold badges443 silver badges486 bronze badges
1
Đã hỏi ngày 7 tháng 2 năm 2014 lúc 6:15
>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
Bạn có thể sử dụng phương pháp str.zfill
tích hợp, như thế này
Từ các tài liệu,
Trả về chuỗi số bên trái chứa đầy số 0 trong một chuỗi chiều rộng. Một tiền tố dấu hiệu được xử lý chính xác. Chuỗi ban đầu được trả về nếu chiều rộng nhỏ hơn hoặc bằng Len [s].Feb 7, 2014 at 6:19
Vì vậy, nếu chiều dài của chuỗi thực tế nhiều hơn chiều rộng được chỉ định [tham số được chuyển cho zfill
], chuỗi được trả về như hiện tại.falsetru
Đã trả lời ngày 7 tháng 2 năm 2014 lúc 6:1658 gold badges686 silver badges609 bronze badges
4
Thefourtheyethefourtheye
>>> for i in range[1,15]:
... print '%0.2d' % i
...
01
02
03
04
05
06
07
08
09
10
11
12
13
14
>>>
Trả về chuỗi số bên trái chứa đầy số 0 trong một chuỗi chiều rộng. Một tiền tố dấu hiệu được xử lý chính xác. Chuỗi ban đầu được trả về nếu chiều rộng nhỏ hơn hoặc bằng Len [s].Feb 7, 2014 at 6:19
Vì vậy, nếu chiều dài của chuỗi thực tế nhiều hơn chiều rộng được chỉ định [tham số được chuyển cho zfill
], chuỗi được trả về như hiện tại.James Sapam
Đã trả lời ngày 7 tháng 2 năm 2014 lúc 6:1611 gold badges47 silver badges68 bronze badges
2
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Example:
Input: GFG Output: 0000GFG Explaination: Added four zeros before GFG.
Bàn luận
Ở đây, chúng tôi sẽ học cách đệm hoặc thêm số 0 hàng đầu vào đầu ra theo yêu cầu. Hãy để thảo luận về những cách nhất định trong đó vấn đề này có thể được giải quyết trong Python.sing rjust[]
Hiển thị một chuỗi với các số không hàng đầu trong Python
Python3
Thêm số 0 hàng đầu vào chuỗi bằng rjust [] & nbsp;
my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
7my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
8my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
9Hàm này cung cấp một cách một dòng để thực hiện nhiệm vụ cụ thể này. Do đó có thể dễ dàng được sử dụng trên bất kỳ chuỗi nào mà chúng tôi cần phải thực hiện. Chúng tôi có thể chỉ định lượng đệm cần thiết. & NBSP;
my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
4my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
5 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
6>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
0____21 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
2. >>> for i in range[1,15]:
... print '%0.2d' % i
...
01
02
03
04
05
06
07
08
09
10
11
12
13
14
>>>
1 >>> for i in range[1,15]:
... print '%0.2d' % i
...
01
02
03
04
05
06
07
08
09
10
11
12
13
14
>>>
2 >>> for i in range[1,15]:
... print '%0.2d' % i
...
01
02
03
04
05
06
07
08
09
10
11
12
13
14
>>>
3 >>> for i in range[1,15]:
... print '%0.2d' % i
...
01
02
03
04
05
06
07
08
09
10
11
12
13
14
>>>
4 >>> for i in range[1,15]:
... print '%0.2d' % i
...
01
02
03
04
05
06
07
08
09
10
11
12
13
14
>>>
5my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
7my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
8 >>> for i in range[1,15]:
... print '%0.2d' % i
...
01
02
03
04
05
06
07
08
09
10
11
12
13
14
>>>
8>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
4my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
5 >>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
6
7>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
5 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
9>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
1>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
The original string : GFG The string after adding leading zeros : 0000GFG
>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
0____21 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
2Input: GFG
Output: 0000GFG
Explaination: Added four zeros before GFG.
2sing
zfill[]
>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
Đầu ra: & nbsp;
Python3
Thêm số 0 hàng đầu vào chuỗi bằng rjust [] & nbsp;
Hàm này cung cấp một cách một dòng để thực hiện nhiệm vụ cụ thể này. Do đó có thể dễ dàng được sử dụng trên bất kỳ chuỗi nào mà chúng tôi cần phải thực hiện. Chúng tôi có thể chỉ định lượng đệm cần thiết. & NBSP;
my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
4my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
5 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
6>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
0____21 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
2.>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
4my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
5 >>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
6
7>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
5 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
9>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
1>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
The original string : GFG The string after adding leading zeros : 0000GFG
>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
0____21 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
2Input: GFG
Output: 0000GFG
Explaination: Added four zeros before GFG.
2sing concatenation
>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
Đầu ra: & nbsp;0 as a string with the number of zeros the user wants and using Python concatenation to merge them all.
Python3
Thêm số 0 hàng đầu vào chuỗi bằng rjust [] & nbsp;
Hàm này cung cấp một cách một dòng để thực hiện nhiệm vụ cụ thể này. Do đó có thể dễ dàng được sử dụng trên bất kỳ chuỗi nào mà chúng tôi cần phải thực hiện. Chúng tôi có thể chỉ định lượng đệm cần thiết. & NBSP;
>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
0my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
2>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
3my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
4my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
5 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
6>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
0____21 my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
2.>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
4my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
5 >>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
6my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
7zfill
1>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
7my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
5 >>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
9>>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
1
0____21 >>> format[1, '02']
'01'
>>> format[100, '02']
'100'
>>> '{:02}'.format[1]
'01'
>>> '{:02}'.format[100]
'100'
2my_string = "1"
print my_string.zfill[2] # Prints 01
my_string = "1000"
print my_string.zfill[2] # Prints 1000
Input: GFG
Output: 0000GFG
Explaination: Added four zeros before GFG.
2
The original string : GFG The string after adding leading zeros : 0000GFG