Hướng dẫn write a python program that accepts a string and calculate the number of digits and letters - viết một chương trình python chấp nhận một chuỗi và tính toán số chữ số và chữ cái

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:50:47 (UTC/GMT +8 giờ)

Python có điều kiện: Bài tập-14 với giải pháp

Viết một chương trình Python chấp nhận một chuỗi và tính toán số chữ số và chữ cái.

Show

Trình bày bằng hình ảnh:

Hướng dẫn write a python program that accepts a string and calculate the number of digits and letters - viết một chương trình python chấp nhận một chuỗi và tính toán số chữ số và chữ cái

Giải pháp mẫu:

Mã Python:

s = input("Input a string")
d=l=0
for c in s:
    if c.isdigit():
        d=d+1
    elif c.isalpha():
        l=l+1
    else:
        pass
print("Letters", l)
print("Digits", d)

Đầu ra mẫu:

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 

Sơ đồ:

Hướng dẫn write a python program that accepts a string and calculate the number of digits and letters - viết một chương trình python chấp nhận một chuỗi và tính toán số chữ số và chữ cái

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Trình chỉnh sửa mã Python:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

Trước đó: Viết chương trình Python chấp nhận một chuỗi các dấu phẩy được phân tách 4 số nhị phân là đầu vào của nó và in các số chia hết cho 5 theo trình tự phân tách dấu phẩy. từ người dùng). Write a Python program which accepts a sequence of comma separated 4 digit binary numbers as its input and print the numbers that are divisible by 5 in a comma separated sequence.
Next: Write a Python program to check the validity of a password (input from users).

Mức độ khó của bài tập này là gì?

Kiểm tra kỹ năng lập trình của bạn với bài kiểm tra của W3Resource.

Python: Lời khuyên trong ngày

Từ điển Hiểu biết:

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}


  • Bài tập: Top 16 chủ đề phổ biến nhất hàng tuần
  • Bài tập SQL, Thực hành, Giải pháp - Tham gia
  • Bài tập SQL, Thực hành, Giải pháp - Quan sát phụ
  • JavaScript Basic - Bài tập, Thực hành, Giải pháp
  • Java Array: Bài tập, Thực hành, Giải pháp
  • C Bài tập lập trình, Thực hành, Giải pháp: Tuyên bố có điều kiện
  • Cơ sở dữ liệu nhân sự - Sắp xếp bộ lọc: Bài tập, Thực hành, Giải pháp
  • C Bài tập lập trình, Thực hành, Giải pháp: Chuỗi
  • Các loại dữ liệu Python: Từ điển - Bài tập, Thực hành, Giải pháp
  • Câu đố lập trình Python - Bài tập, Thực hành, Giải pháp
  • Mảng C ++: Bài tập, Thực hành, Giải pháp
  • Báo cáo và vòng lặp có điều kiện JavaScript - Bài tập, Thực hành, Giải pháp
  • Thuật toán cơ bản C# Sharp: Bài tập, Thực hành, Giải pháp
  • Python Lambda - Bài tập, Thực hành, Giải pháp
  • Python Pandas DataFrame: Bài tập, Thực hành, Giải pháp
  • Công cụ chuyển đổi
  • JavaScript: HTML Mẫu xác thực


Đưa ra một chuỗi, chứa các chữ số và chữ cái, nhiệm vụ là viết một chương trình Python để tính toán số chữ số và chữ cái trong một chuỗi. & NBSP;

Example:

Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.

Phương pháp 1: Sử dụng phương thức tích hợp isalpha ()

Python3

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
3
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
4

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
5
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
6
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
8

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
9
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
0
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
1

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
3
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
9
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
1
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
2
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
3
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
4

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
7
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
8

Output:

Number of Digit is 4
Number of Alphabets is 5

Explanation:

Ở đây chúng tôi đã sử dụng phương thức tích hợp isalpha (), thường giúp chúng tôi xác định xem nhân vật cụ thể đó có phải là bảng chữ cái hay không và nếu nó không thì chúng tôi chỉ đơn giản bỏ qua nó. Giả sử điều kiện chuỗi chỉ tạo thành bảng chữ cái và chữ số thì chúng ta có thể kết luận rằng liệu ký tự đó sẽ là một chữ số hay bảng chữ cái. Chúng tôi đã có số lượng của tất cả các bảng chữ cái sau đó chúng tôi có thể trừ đi số lượng với độ dài của chuỗi và do đó chúng tôi có thể nhận được số chữ số.

Phương pháp 2: Sử dụng tất cả các chữ số và tất cả các danh sách chữ cái

Python3

Các

Các

Total letters found :- 13
Total digits found :- 2
9
Total letters found :- 13
Total digits found :- 2
0
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0 ____9230________
Total letters found :- 13
Total digits found :- 2
4____________
Total letters found :- 13
Total digits found :- 2
________________
Total letters found :- 13
Total digits found :- 2
8__________________

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
18
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
20

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
21
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
2

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
24
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
2

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
5
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
28
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
8

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
9
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
28
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
35

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
21
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
9
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
42
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
28
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
45

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
24
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
53
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
54

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
57
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
58

Output:

Total letters found :- 13
Total digits found :- 2

Explanation:

Ý tưởng ở đây là để giải quyết vấn đề này bằng cách lặp qua tất cả các ký tự và kiểm tra xem ký tự có trong All_Digits lưu trữ tất cả các chữ số hoặc All_letters lưu trữ tất cả các bảng chữ cái trong danh sách. & NBSP;

Phương pháp 3: Bằng cách chỉ kiểm tra một trong các điều kiện trên

Python3

Các

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
72
Total letters found :- 13
Total digits found :- 2
2
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 13
Total digits found :- 2
4
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 13
Total digits found :- 2
6
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 13
Total digits found :- 2
8
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 13
Total digits found :- 2
0
Total letters found :- 13
Total digits found :- 2
1

Total letters found :- 13
Total digits found :- 2
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Number of Digit is 4
Number of Alphabets is 5
1
Total letters found :- 13
Total digits found :- 2
5____________
Total letters found :- 13
Total digits found :- 2
7
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 13
Total digits found :- 2
9____________
Total letters found :- 6
Total digits found :- 4
1
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
030

Total letters found :- 13
Total digits found :- 2
9
Total letters found :- 6
Total digits found :- 4
7
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 6
Total digits found :- 4
9
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 13
Total digits found :- 2
1
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 13
Total digits found :- 2
3
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 13
Total digits found :- 2
5
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Total letters found :- 13
Total digits found :- 2
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
3

Các

Total letters found :- 13
Total digits found :- 2
9
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
02
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
04
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
06
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
08
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
10
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
12
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
14
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
16
Total letters found :- 13
Total digits found :- 2
1

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
18
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
20

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
21
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
2

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
24
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
2

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
5
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
28
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
8

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
9
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
28
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
35

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
21
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
9
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
65
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
66

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
24
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
53
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
54

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
57
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
58

Output:

Total letters found :- 13
Total digits found :- 2

Explanation:

Thay vì kiểm tra các ký tự trong all_letters, chúng ta có thể kiểm tra: & nbsp; & nbsp;

  • Nếu ký tự được tìm thấy trong tất cả các chữ số, thì hãy tăng giá trị Total_Digits thêm một
  • Nếu không có nghĩa là ký tự là một chữ cái, giá trị Total_letters tăng lên một

Phương pháp 3: Bằng cách sử dụng phương thức tích hợp isnumeric ()

Python3

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
18
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
82

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
21
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
2

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
24
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
2

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
5
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
28
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
8

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
9
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
28
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
35

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
21
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
9
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
65
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
66

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
24
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
53
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
54

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
57
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
58

Output:

Total letters found :- 6
Total digits found :- 4

Explanation:

Thay vì kiểm tra các ký tự trong all_letters, chúng ta có thể kiểm tra: & nbsp; & nbsp;

Nếu ký tự được tìm thấy trong tất cả các chữ số, thì hãy tăng giá trị Total_Digits thêm một

Python3

Nếu không có nghĩa là ký tự là một chữ cái, giá trị Total_letters tăng lên một

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
18
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
20

Phương pháp 3: Bằng cách sử dụng phương thức tích hợp isnumeric ()

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
18
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
82

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
53
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
54

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
57
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
58

Output:

Total letters found :- 13
Total digits found :- 2

Explanation:

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
9
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
0
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
95

Ý tưởng ở đây là giải quyết vấn đề này bằng cách lặp qua tất cả các ký tự và kiểm tra xem ký tự là chữ cái hoặc chữ số sử dụng hàm isnumeric (). Nếu isnumeric () là đúng, điều đó có nghĩa là một nhân vật là một chữ số, thì nhân vật khác là một chữ cái.

Phương pháp 4: Sử dụng hàm Re.Findall () và Len ()O(n)

Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
17
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.
18
O(n)

Input a string W3resource Letters 9 Digits 1 21Input a string W3resource Letters 9 Digits 1 1 Input: string = "geeks2for3geeks" Output: total digits = 2 and total letters = 13 Input: string = "python1234" Output: total digits = 4 and total letters = 6 Input: string = "co2mpu1te10rs" Output: total digits = 4 and total letters = 9 Explanation: Here we are calculating the number of digits and alphabets in the given string.1Input: string = "geeks2for3geeks" Output: total digits = 2 and total letters = 13 Input: string = "python1234" Output: total digits = 4 and total letters = 6 Input: string = "co2mpu1te10rs" Output: total digits = 4 and total letters = 9 Explanation: Here we are calculating the number of digits and alphabets in the given string.25Input: string = "geeks2for3geeks" Output: total digits = 2 and total letters = 13 Input: string = "python1234" Output: total digits = 4 and total letters = 6 Input: string = "co2mpu1te10rs" Output: total digits = 4 and total letters = 9 Explanation: Here we are calculating the number of digits and alphabets in the given string.26Input: string = "geeks2for3geeks" Output: total digits = 2 and total letters = 13 Input: string = "python1234" Output: total digits = 4 and total letters = 6 Input: string = "co2mpu1te10rs" Output: total digits = 4 and total letters = 9 Explanation: Here we are calculating the number of digits and alphabets in the given string.27Using ord() function

Python3

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
18
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
20

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
21
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
2

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
24
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
2

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
5
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
28
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
8

Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
9
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
0
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
28
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
7
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
35

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
21
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
24
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
24
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
4
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
1
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
6

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
53
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
54

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
7
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
8
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
57
Input a string W3resource                                                                                                     
Letters 9                                                                                                                     
Digits 1 
58

Output:

Total letters found :- 13
Total digits found :- 2

Explanation:

Thay vì kiểm tra các ký tự trong all_letters, chúng ta có thể kiểm tra: & nbsp; & nbsp;


Làm thế nào để bạn tìm thấy số lượng chữ cái và số trong một chuỗi trong Python?

Cách đơn giản nhất là sử dụng chức năng tích hợp trong chức năng, Len ().Nếu Len (A)> 0: Vì một chuỗi trống không có độ dài.use the built in function, len() . if len(a) > 0: since an empty string has no length.

Làm thế nào để bạn tìm thấy số lượng chữ cái trong một chuỗi trong Python?

Trong Python, bạn có thể có độ dài của chuỗi str (= số lượng ký tự) với hàm tích hợp len ().with the built-in function len() .

Làm thế nào để bạn tìm thấy số chữ số trong Python?

Nếu bạn muốn độ dài của một số nguyên như trong số lượng các chữ số trong số nguyên, bạn luôn có thể chuyển đổi nó thành chuỗi như str (133) và tìm độ dài của nó như len (str (123)).len(str(123)) .

Làm thế nào để bạn tìm thấy số chữ số trong một chuỗi?

Để đếm số chữ số trong một chuỗi, hãy sử dụng phương thức thay thế () để thay thế tất cả các ký tự không chữ số bằng một chuỗi trống và truy cập thuộc tính độ dài trên kết quả.Phương thức thay thế trả về một chuỗi mới với các trận đấu được thay thế.use the replace() method to replace all non digit characters with an empty string and access the length property on the result. The replace method returns a new string with the matches replaced.