Hướng dẫn how do you add a special character to a password in python? - làm cách nào để thêm ký tự đặc biệt vào mật khẩu trong python?

Tôi muốn người dùng sử dụng các ký tự đặc biệt khi tạo mật khẩu. [Ít nhất 1] Ví dụ:

special_character= list[string.punctuation]
password=input["enter password:"]
if any[i not in password for i in special_character]:
    print["You should add a special character for creating strong password!"]

Nó sẽ như thế này nhưng nó không hoạt động. Bạn có thể giúp tôi được không? Cảm ơn.

hỏi ngày 17 tháng 4 lúc 18:18Apr 17 at 18:18

Chỉ cần sử dụng all[...], hoặc cách khác, sử dụng not any[...] nhưng không có not trong danh sách hiểu [điều này tốt hơn vì not được chạy ít lần hơn về tổng thể, làm cho nó hiệu quả hơn một chút]. Đây là những gì mã sẽ trông như thế nào:

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]

Đã trả lời ngày 17 tháng 4 lúc 18:21Apr 17 at 18:21

LecdilecdiLecdi

2.0062 huy hiệu vàng4 Huy hiệu bạc17 Huy hiệu đồng2 gold badges4 silver badges17 bronze badges

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
81
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
83
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
84
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
85

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
87
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
89

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
8
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
92

Bạn có thể sử dụng các ký tự đặc biệt trong mật khẩu không?

  1. Mật khẩu sẽ tận dụng số lượng ký tự tối đa cho phép của mỗi tài khoản. Nó phải chứa tất cả các ký tự mà tài khoản cho phép, ví dụ: Số, dấu câu và ký tự đặc biệt.
  2. Các nhân vật đặc biệt có được phép trong Python không?
  3. Python3. Một định danh trong Python không thể sử dụng bất kỳ biểu tượng đặc biệt nào như!, @, #, $, %, V.v.
  4. Cải thiện bài viết
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!

Lưu bài viết

Hãy để lấy mật khẩu làm kết hợp các ký tự chữ và số cùng với các ký tự đặc biệt và kiểm tra xem mật khẩu có hợp lệ hay không với sự trợ giúp của một số điều kiện. Naive Method [Without using Regex].

Điều kiện cho một mật khẩu hợp lệ là:

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
2
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
4
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
5
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
6
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
7
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
6
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
9
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
6
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
1
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
2

Nên có ít nhất một số.

Nên có ít nhất một ký tự chữ hoa và một ký tự viết thường.

Password is valid
3
Password is valid
4
Password is valid
5
Password is valid
6
Password is valid
7

Password is valid
3
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
4
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
Password is valid.
1

Nên có ít nhất một biểu tượng đặc biệt.

Password is valid
3
Password is valid
4
Password is valid
5all[...]1
Password is valid
7

Password is valid
3
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
4
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
Password is valid.
1

Nên dài từ 6 đến 20 ký tự.

Password is valid
3
Password is valid
4
Password is valid
5not any[...]9
Password is valid
7

Password is valid
3
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
4
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
Password is valid.
1

Chúng tôi có thể kiểm tra xem một chuỗi đã cho có đủ điều kiện để trở thành mật khẩu hoặc không sử dụng nhiều cách.

Phương pháp số 1: Phương pháp ngây thơ [không sử dụng Regex].

Password is valid
3
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
4
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
Password is valid.
1

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
8 not not any[...]0def7not any[...]2 not any[...]3__

Password is valid
3
Password is valid
4
Password is valid
5
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
05
Password is valid
7

Password is valid
3
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
4
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
Password is valid.
1

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
8 not not any[...]0
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
15not any[...]4
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
2not any[...]2

Password is valid
3
Password is valid
4
Password is valid
5
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
25
Password is valid
7

Password is valid
3
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
4
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
Password is valid.
1

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
8
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
33

Password is valid
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
35
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
36

def

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
38

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
40
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
42

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
8
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
45

Password is valid
3
Password is valid
4
Password is valid
5
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
49
Password is valid
7

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
52
Password is valid
2

Password is valid
3
Password is valid
4
Password is valid
5
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
57
Password is valid
7

Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
8
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
60
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
63
Password is valid
2

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
66

Output:

Password is valid

Mã này đã sử dụng các chức năng Boolean để kiểm tra xem tất cả các điều kiện có được thỏa mãn hay không. Chúng tôi thấy rằng mặc dù độ phức tạp của mã là cơ bản, độ dài là đáng kể. & NBSP; Phương pháp #2: Sử dụng Regex
 
Method #2: Using regex

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
67 Phương pháp mô -đun Regex tạo ra một đối tượng Regex, giúp thực thi các hàm regex lên biến PAT. Sau đó, chúng tôi kiểm tra xem mẫu được xác định bởi PAT có được theo sau bởi chuỗi đầu vào passwd không. Nếu vậy, phương thức tìm kiếm trả về true, cho phép mật khẩu hợp lệ.

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
68
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
69

def

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
38

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
40
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
42

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
777____13
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
79

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
81
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
83
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
84
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
85

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
87
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
89

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
8
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
92

Password is valid
3
Password is valid
4
Password is valid
5
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
96
Password is valid
7

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
52
Password is valid
2

Password is valid
3
Password is valid
4
Password is valid
5
Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
04
Password is valid
7

Input :  Geek12#
Output : Password is valid.

Input :  asd123
Output : Invalid Password !!
8
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
60
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
3
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
63
Password is valid
2

special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
1
special_characters = list[string.punctuation]
password = input["Enter password: "]
if not any[i in password for i in special_characters]:
    print["You should add a special character for creating strong password!"]
66

Output:

Password is valid.


Bạn có thể sử dụng các ký tự đặc biệt trong mật khẩu không?

Mật khẩu sẽ tận dụng số lượng ký tự tối đa cho phép của mỗi tài khoản. Nó phải chứa tất cả các ký tự mà tài khoản cho phép, ví dụ: Số, dấu câu và ký tự đặc biệt.It should contain all the characters that the account allows, e.g. numbers, punctuation and special characters.

Các nhân vật đặc biệt có được phép trong Python không?

Python3. Một định danh trong Python không thể sử dụng bất kỳ biểu tượng đặc biệt nào như!, @, #, $, %, V.v.cannot use any special symbols like !, @, #, $, % etc.

Bài Viết Liên Quan

Chủ Đề