Kiểm tra chữ và số trong python

Python cung cấp các phương thức để kiểm tra xem tất cả các ký tự trong chuỗi

s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
5 là số, chữ cái, chữ số hay ASCII

Bài viết này mô tả các nội dung sau

  • Kiểm tra xem một chuỗi chỉ chứa số thập phân.
    s = '-1.23'
    print['s =', s]
    print['isdecimal:', s.isdecimal[]]
    print['isdigit:', s.isdigit[]]
    print['isnumeric:', s.isnumeric[]]
    # s = -1.23
    # isdecimal: False
    # isdigit: False
    # isnumeric: False
    
    6
  • Kiểm tra xem một chuỗi chỉ chứa các chữ số.
    s = '-1.23'
    print['s =', s]
    print['isdecimal:', s.isdecimal[]]
    print['isdigit:', s.isdigit[]]
    print['isnumeric:', s.isnumeric[]]
    # s = -1.23
    # isdecimal: False
    # isdigit: False
    # isnumeric: False
    
    7
  • Kiểm tra xem một chuỗi chỉ chứa số.
    s = '-1.23'
    print['s =', s]
    print['isdecimal:', s.isdecimal[]]
    print['isdigit:', s.isdigit[]]
    print['isnumeric:', s.isnumeric[]]
    # s = -1.23
    # isdecimal: False
    # isdigit: False
    # isnumeric: False
    
    8
  • Kiểm tra xem một chuỗi chỉ chứa chữ cái.
    s = '-1.23'
    print['s =', s]
    print['isdecimal:', s.isdecimal[]]
    print['isdigit:', s.isdigit[]]
    print['isnumeric:', s.isnumeric[]]
    # s = -1.23
    # isdecimal: False
    # isdigit: False
    # isnumeric: False
    
    9
  • Kiểm tra xem một chuỗi chỉ chứa chữ và số.
    s = '10\u00B2'
    print['s =', s]
    print['isdecimal:', s.isdecimal[]]
    print['isdigit:', s.isdigit[]]
    print['isnumeric:', s.isnumeric[]]
    # s = 10²
    # isdecimal: False
    # isdigit: True
    # isnumeric: True
    
    0
  • Kiểm tra xem một chuỗi chỉ chứa ASCII.
    s = '10\u00B2'
    print['s =', s]
    print['isdecimal:', s.isdecimal[]]
    print['isdigit:', s.isdigit[]]
    print['isnumeric:', s.isnumeric[]]
    # s = 10²
    # isdecimal: False
    # isdigit: True
    # isnumeric: True
    
    1
  • Kiểm tra xem một chuỗi có trống không
  • Kiểm tra xem một chuỗi có phải là một số không [= có thể được chuyển đổi thành giá trị số]

Đối với các phương thức không phải là

s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
2, chuỗi rỗng và chuỗi chứa ký hiệu [
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
3,
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
4,
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
5, v.v. ] trả lại
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
6. Phần cuối cùng mô tả cách kiểm tra
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
7, ví dụ, là một giá trị số

Xem bài viết sau để biết cách chuyển đổi chuỗi

s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
5 thành số
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
9 và
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
0

  • Chuyển chuỗi thành số [int, float] trong Python

Liên kết được tài trợ

Kiểm tra xem một chuỗi chỉ chứa số thập phân.
s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
6

s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
2 trả về
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 nếu tất cả các ký tự là ký tự thập phân trong danh mục chung Unicode
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
4. Số toàn chiều rộng của CJK cũng được xác định là
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3

s = '1234567890'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 1234567890
# isdecimal: True
# isdigit: True
# isnumeric: True

s = '1234567890'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 1234567890
# isdecimal: True
# isdigit: True
# isnumeric: True

nguồn.

Một chuỗi chứa các ký hiệu như

s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
5 và
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
4 được xác định là
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
6

s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False

nguồn.

Nếu bạn muốn xác định một chuỗi chẳng hạn như

s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
9 dưới dạng số, bạn có thể sử dụng xử lý ngoại lệ. Nó được mô tả trong phần cuối

Kiểm tra xem một chuỗi chỉ chứa các chữ số.
s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
7

s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
1 trả về
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 không chỉ cho các ký tự là
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 với
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
2 mà còn cho các ký tự có giá trị thuộc tính Unicode
s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
5 là
s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
6 hoặc
s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
7

Ví dụ: số siêu ký tự

s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
8 [
s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
9] là
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
6 trong
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
2, nhưng là
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 trong
s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
1

s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True

nguồn.

Kiểm tra xem một chuỗi chỉ chứa số.
s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
8

s = 'abc123'
print['s =', s]
print['isalnum:', s.isalnum[]]
print['isalpha:', s.isalpha[]]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = abc123
# isalnum: True
# isalpha: False
# isdecimal: False
# isdigit: False
# isnumeric: False
5 trả về
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 không chỉ cho các ký tự là
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 với
s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
1 mà còn cho các ký tự có giá trị thuộc tính Unicode
s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
5 là
s = 'abc123+-,.&'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = abc123+-,.&
# isascii: True
# isalnum: False
0

Phân số thô tục, chữ số La Mã, chữ số Trung Quốc, v.v. cũng được xác định là

s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3

s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True

nguồn.

Kiểm tra xem một chuỗi chỉ chứa chữ cái.
s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
9

s = 'abc123+-,.&'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = abc123+-,.&
# isascii: True
# isalnum: False
3 trả về
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 nếu tất cả các ký tự trong chuỗi là chữ cái. Các ký tự chữ cái là những ký tự được định nghĩa trong cơ sở dữ liệu ký tự Unicode là
s = 'abc123+-,.&'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = abc123+-,.&
# isascii: True
# isalnum: False
5, i. e. , những người có tài sản loại chung là một trong số
s = 'abc123+-,.&'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = abc123+-,.&
# isascii: True
# isalnum: False
6,
s = 'abc123+-,.&'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = abc123+-,.&
# isascii: True
# isalnum: False
7,
s = 'abc123+-,.&'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = abc123+-,.&
# isascii: True
# isalnum: False
8,
s = 'abc123+-,.&'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = abc123+-,.&
# isascii: True
# isalnum: False
9 hoặc
s = 'あいうえお'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = あいうえお
# isascii: False
# isalnum: True
0

Không chỉ bảng chữ cái Latinh, mà cả các ký tự của các ngôn ngữ khác, chẳng hạn như chữ hiragana của Nhật Bản, được xác định là

s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3

s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True

nguồn.

Liên kết được tài trợ

Kiểm tra xem một chuỗi chỉ chứa chữ và số.
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
0

s = 'あいうえお'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = あいうえお
# isascii: False
# isalnum: True
3 trả về
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 nếu mỗi ký tự là
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 với một trong các phương thức được liệt kê cho đến nay,
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
2,
s = 'abc'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = abc
# isalpha: True

s = 'あいうえお'
print['s =', s]
print['isalpha:', s.isalpha[]]
# s = あいうえお
# isalpha: True
1,
s = 'abc123'
print['s =', s]
print['isalnum:', s.isalnum[]]
print['isalpha:', s.isalpha[]]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = abc123
# isalnum: True
# isalpha: False
# isdecimal: False
# isdigit: False
# isnumeric: False
5 và
s = 'abc123+-,.&'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = abc123+-,.&
# isascii: True
# isalnum: False
3

Vì mỗi ký tự được đánh giá riêng lẻ, một chuỗi chứa chữ cái và số được xác định là

s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 trong
s = 'あいうえお'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = あいうえお
# isascii: False
# isalnum: True
3 ngay cả khi
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
6 trong tất cả các phương pháp khác

s = 'abc123'
print['s =', s]
print['isalnum:', s.isalnum[]]
print['isalpha:', s.isalpha[]]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = abc123
# isalnum: True
# isalpha: False
# isdecimal: False
# isdigit: False
# isnumeric: False

nguồn.

Kiểm tra xem một chuỗi chỉ chứa ASCII.
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
1

Trong Trăn 3. 7,

s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
2 đã được thêm vào.
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
2 trả về
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 nếu tất cả các ký tự trong chuỗi là ký tự ASCII [U+0000 - U+007F]

  • ASCII - Wikiwand

Các ký hiệu như

s = ''
print['s =', s]
print['isalnum:', s.isalnum[]]
print['isalpha:', s.isalpha[]]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
print['isascii:', s.isascii[]]
# s = 
# isalnum: False
# isalpha: False
# isdecimal: False
# isdigit: False
# isnumeric: False
# isascii: True
7 và
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
5 cũng được xác định là
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3

s = 'abc123+-,.&'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = abc123+-,.&
# isascii: True
# isalnum: False

nguồn.

Hiragana, v.v. , không phải ASCII, được xác định là

s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
6

s = 'あいうえお'
print['s =', s]
print['isascii:', s.isascii[]]
print['isalnum:', s.isalnum[]]
# s = あいうえお
# isascii: False
# isalnum: True

nguồn.

Không giống như các phương thức khác,

s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
2 trả về
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 ngay cả đối với các chuỗi trống, như được giải thích tiếp theo

Kiểm tra xem một chuỗi có trống không

Chuỗi rỗng

print[bool['']]
# False

print[bool['abc123']]
# True
3 được xác định là
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 bởi
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
2 và là
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
6 bởi các phương pháp khác

s = ''
print['s =', s]
print['isalnum:', s.isalnum[]]
print['isalpha:', s.isalpha[]]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
print['isascii:', s.isascii[]]
# s = 
# isalnum: False
# isalpha: False
# isdecimal: False
# isdigit: False
# isnumeric: False
# isascii: True

nguồn.

Sử dụng

print[bool['']]
# False

print[bool['abc123']]
# True
7 để kiểm tra xem một chuỗi có rỗng hay không. Nó trả về
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
6 cho một chuỗi rỗng và
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 cho những chuỗi khác

  • Chuyển đổi bool [True, False] và các loại khác với nhau trong Python

print[bool['']]
# False

print[bool['abc123']]
# True

nguồn.

Kiểm tra xem một chuỗi có phải là một số không [= có thể được chuyển đổi thành giá trị số]

Một số âm hoặc giá trị thập phân chứa

s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
4 hoặc
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
5, vì vậy chúng được xác định là
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
6 đối với các phương pháp khác với
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
2

Mặc dù

s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
2 trả về
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3, nhưng nó không phù hợp để kiểm tra xem một chuỗi có phải là số hay không [= có thể được chuyển đổi thành giá trị số], bởi vì nó trả về
s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 ngay cả khi bao gồm các ký hiệu hoặc bảng chữ cái khác

s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
0

nguồn.

Một chuỗi

s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
5 có thể được chuyển đổi thành một số dấu phẩy động với
s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
08. Đã xảy ra lỗi đối với các chuỗi không thể chuyển đổi thành số

  • Chuyển chuỗi thành số [int, float] trong Python

s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
1

nguồn.

Với việc xử lý ngoại lệ, bạn có thể định nghĩa một hàm trả về

s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3 khi một chuỗi có thể được chuyển đổi bằng
s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
08

  • "cố gắng. ngoại trừ. khác. cuối cùng. " bằng Python

s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
2

nguồn.

Nếu bạn muốn xác định chuỗi chứa dấu tách nhóm chữ số là

s = '\u00BD'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '\u2166'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True

s = '一二三四五六七八九〇'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
3, hãy sử dụng
s = '-1.23'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
12 để loại bỏ chúng bằng cách thay thế
s = '10\u00B2'
print['s =', s]
print['isdecimal:', s.isdecimal[]]
print['isdigit:', s.isdigit[]]
print['isnumeric:', s.isnumeric[]]
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
3 bằng chuỗi trống
print[bool['']]
# False

print[bool['abc123']]
# True
3

Làm cách nào để kiểm tra chữ và số bằng regex?

Regex chữ và số và dấu gạch dưới . Vâng đúng. The regex \w is equivalent to [A-Za-z0-9_] , matches alphanumeric characters and underscore. Yes, true.

Kiểm tra chữ và số là gì?

Cho chuỗi str, nhiệm vụ là kiểm tra xem chuỗi có phải là chữ và số hay không bằng cách sử dụng Biểu thức chính quy. Chuỗi chữ và số là chuỗi chỉ chứa các chữ cái từ a-z, A-Z và một số số từ 0-9 .

Chủ Đề