Ví dụ 1: Số lượng chữ số trong một số nguyên sử dụng trong khi vòng lặp
num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
Đầu ra
Number of digits: 4
Trong chương trình này, vòng lặp trong khi được lặp lại cho đến khi biểu thức kiểm tra
Number of digits: 43 được đánh giá thành 0 [sai].
- Sau lần lặp đầu tiên,
Number of digits: 4
4 sẽ được chia cho 10 và giá trị của nó sẽ là 345. Sau đó,Number of digits: 4
5 được tăng lên 1. - Sau lần lặp thứ hai, giá trị của
Number of digits: 4
4 sẽ là 34 vàNumber of digits: 4
5 được tăng lên 2. - Sau lần lặp thứ ba, giá trị của
Number of digits: 4
4 sẽ là 3 vàNumber of digits: 4
5 được tăng lên 3. - Sau lần lặp thứ tư, giá trị của
Number of digits: 4
4 sẽ là 0 vàNumber of digits: 4
5 được tăng lên 4. - Sau đó, biểu thức kiểm tra được đánh giá là sai và vòng lặp chấm dứt.
Ví dụ 2: Sử dụng các phương thức sẵn có
num = 123456
print[len[str[num]]]
Đầu ra
6
Trong chương trình này, vòng lặp trong khi được lặp lại cho đến khi biểu thức kiểm tra
Number of digits: 43 được đánh giá thành 0 [sai].
string.isdigit[]3
string.isdigit[]4
string.isdigit[]0
string.isdigit[]6
string.isdigit[]1
string.isdigit[]8
Phương thức
num = 123456
print[len[str[num]]]
4 trả về num = 123456
print[len[str[num]]]
5 nếu tất cả các ký tự trong chuỗi là các chữ số. Nếu không, nó trả về num = 123456
print[len[str[num]]]
6.Thí dụ
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
Cú pháp của chuỗi isDigit []
Cú pháp của
num = 123456
print[len[str[num]]]
4 làstring.isdigit[]
Các tham số isDigit []
num = 123456
print[len[str[num]]]
4 không lấy bất kỳ tham số nào.Giá trị trả về từ isDigit []
num = 123456
print[len[str[num]]]
4 trả về:- Đúng nếu tất cả các ký tự trong chuỗi là các chữ số. if all characters in the string are digits.
- Sai nếu ít nhất một ký tự không phải là một chữ số. if at least one character is not a digit.
Ví dụ 1: Làm việc của isDigit []
s = "28212"
print[s.isdigit[]]
# contains alphabets and spaces
s = "Mo3 nicaG el l22er"
print[s.isdigit[]]
Đầu ra
True False
Một chữ số là một ký tự có giá trị thuộc tính:
6
06
1
Trong Python, SuperScript và các chỉ số [thường được viết bằng Unicode] cũng được coi là ký tự chữ số. Do đó, nếu chuỗi chứa các ký tự này cùng với các ký tự thập phân,
num = 123456
print[len[str[num]]]
4 trả về đúng.Các chữ số La Mã, tử số tiền tệ và phân số [thường được viết bằng unicode] được coi là ký tự số nhưng không phải là chữ số.
num = 123456
print[len[str[num]]]
4 trả về sai nếu chuỗi chứa các ký tự này.Để kiểm tra xem một ký tự có phải là ký tự số hay không, bạn có thể sử dụng phương thức isnumeric [].
Ví dụ 2: Chuỗi chứa các chữ số và ký tự số
s = '23455'
print[s.isdigit[]]
#s = '²3455'
# subscript is a digit
s = '\u00B23455'
print[s.isdigit[]]
# s = '½'
# fraction is not a digit
s = '\u00BD'
print[s.isdigit[]]
Đầu ra
True True False
print[str1.isdigit[]] print[str2.isdigit[]]True
True
False
7
9 str1 = '342'
Number of digits: 4
04string.isdigit[]
1 Number of digits: 4
06Number of digits: 4
07
print[str1.isdigit[]] print[str2.isdigit[]] print[s.isdigit[]] print[s.isdigit[]]
8str1 = '342'
0 s = "28212"
Number of digits: 4
10
Number of digits: 411
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
0 Number of digits: 413
Number of digits: 414
Number of digits: 415
64 is a pre-initialized string used as string constant. In Python,
64 will give the lowercase letters ‘0123456789’.
Cải thiện bài viếtstring.digits
Lưu bài viết Doesn’t take any parameter, since it’s not a function.
Trong Python3,
64 là một chuỗi được khởi tạo trước được sử dụng làm hằng số chuỗi. Trong Python,64 sẽ đưa ra các chữ cái viết thường ‘0123456789. Return all digit letters.
Cú pháp: String.digits Make sure to import string library function inorder to use
64
Mã số 1:
67
68
69
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
0 str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
1str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
2str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
3Đầu ra:
Number of digits: 40
& nbsp; mã số 2: Đã cho mã kiểm tra nếu đầu vào chuỗi chỉ có chữ số
Code #2 : Given code checks if the string input has only digit letters
67
68
69
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
0 str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
1Đầu ra:
& nbsp; mã số 2: Đã cho mã kiểm tra nếu đầu vào chuỗi chỉ có chữ số
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
6 str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
7str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
8str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
9 string.isdigit[]0
string.isdigit[]1
string.isdigit[]2
string.isdigit[]3
string.isdigit[]4
string.isdigit[]0
string.isdigit[]6
string.isdigit[]1
string.isdigit[]8
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
2s = "28212"
print[s.isdigit[]]
# contains alphabets and spaces
s = "Mo3 nicaG el l22er"
print[s.isdigit[]]
9True False0
True False1
string.isdigit[]9
s = "28212"
print[s.isdigit[]]
# contains alphabets and spaces
s = "Mo3 nicaG el l22er"
print[s.isdigit[]]
0 num = 123456
print[len[str[num]]]
6str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
2True False6
True False0
True False8
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
8s = "28212"
print[s.isdigit[]]
# contains alphabets and spaces
s = "Mo3 nicaG el l22er"
print[s.isdigit[]]
0 num = 123456
print[len[str[num]]]
5str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
2s = '23455'
print[s.isdigit[]]
#s = '²3455'
# subscript is a digit
s = '\u00B23455'
print[s.isdigit[]]
# s = '½'
# fraction is not a digit
s = '\u00BD'
print[s.isdigit[]]
3True False0
s = '23455'
print[s.isdigit[]]
#s = '²3455'
# subscript is a digit
s = '\u00B23455'
print[s.isdigit[]]
# s = '½'
# fraction is not a digit
s = '\u00BD'
print[s.isdigit[]]
5Output:
Number of digits: 41
print[s.isdigit[]] print[s.isdigit[]]s = "28212"
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
0 s = "28212"
print[s.isdigit[]]
# contains alphabets and spaces
s = "Mo3 nicaG el l22er"
print[s.isdigit[]]
7The string constant digits can be used in many practical applications. Let’s see a code explaining how to use digits to generate strong random passwords of given size.
True False2
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
0 True False4
67
68
True False9
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
0 s = '23455'
print[s.isdigit[]]
#s = '²3455'
# subscript is a digit
s = '\u00B23455'
print[s.isdigit[]]
# s = '½'
# fraction is not a digit
s = '\u00BD'
print[s.isdigit[]]
1Ứng dụng: Các chữ số hằng số chuỗi có thể được sử dụng trong nhiều ứng dụng thực tế. Hãy cùng xem một mã giải thích cách sử dụng các chữ số để tạo mật khẩu ngẫu nhiên mạnh có kích thước đã cho.
True True False7
True True False8
True True False6
True True False7
Number of digits: 401
67
s = '23455'
print[s.isdigit[]]
#s = '²3455'
# subscript is a digit
s = '\u00B23455'
print[s.isdigit[]]
# s = '½'
# fraction is not a digit
s = '\u00BD'
print[s.isdigit[]]
7str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
6 True True False1
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
8True True False3
str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
0 ________ 95 ________ 96 & nbsp;str1 = '342'
print[str1.isdigit[]]
str2 = 'python'
print[str2.isdigit[]]
# Output: True
# False
2Number of digits: 417
Output:
Number of digits: 42