您當前的位置:首頁 > 繪畫

python 判斷大小寫字母、數字

作者:由 希望 發表于 繪畫時間:2022-10-08

方法一:呼叫STRING包

import

string

string

ascii_uppercase

#ABCDEFGHIJKLMNOPQRSTUVWXYZ

string

ascii_lowercase

#abcdefghijklmnopqrstuvwxyz

string

digits

#0123456789

例子:

# 密碼要求:

# 1。長度超過8位

# 2。包括大小寫字母。數字。其它符號,以上四種至少三種

# 3。不能有長度大於2的不含公共元素的子串重複 (注:其他符號不含空格或換行)

import

string

pw_list

=

[]

upper

lower

digit

other

=

0

0

0

0

pw_str

=

input

()

if

len

pw_str

<=

8

print

‘NG’

else

for

i

in

pw_str

if

i

in

string

ascii_uppercase

upper

=

1

elif

i

in

string

ascii_lowercase

lower

=

1

elif

i

in

string

digits

digit

=

1

else

other

=

1

if

upper

+

lower

+

digit

+

other

<

3

print

‘NG’

else

label

=

0

for

i

in

range

len

pw_str

-

1

):

if

pw_str

i

i

+

3

in

pw_str

i

+

3

:]

and

label

==

0

print

‘NG’

label

=

1

if

label

==

0

print

‘OK’

方法二:isupper(),islower(),istitle()方法用來判斷字串的大小寫

#isupper() 判斷是否全是大寫

str

isupper

()

#islower() 判斷是否全是小寫

str

islower

()

#判斷首字母是否大寫,其餘的是否小寫

str

istitle

()

方法三:正則表示式

numberRegex

=

re

compile

r

‘[0-9]’

upperRegex

=

re

compile

r

‘[A-Z]’

lowerRegex

=

re

compile

r

‘[a-z]’

symbolRegex

=

re

compile

r

‘[`~!@#$%^&*()\-_=+[

{}

\]

\\

|;:

\’

“,/?]‘

#呼叫

upperRegex

search

string

例子

import

re

def

length

string

):

if

len

string

>

8

return

True

else

return

False

def

symbol

string

):

numberRegex

=

re

compile

r

’[0-9]‘

upperRegex

=

re

compile

r

’[A-Z]‘

lowerRegex

=

re

compile

r

’[a-z]‘

symbolRegex

=

re

compile

r

’[`~!@#$%^&*()\-_=+[

{}

\]

\\

|;:

\‘

”,/?]’

t

=

0

if

numberRegex

search

string

):

t

+=

1

if

upperRegex

search

string

):

t

+=

1

if

lowerRegex

search

string

):

t

+=

1

if

symbolRegex

search

string

):

t

+=

1

if

t

>=

3

return

True

else

return

False

def

repeatedChildString

string

):

List

=

[]

for

i

in

range

len

string

-

2

):

for

j

in

range

i

+

3

len

string

-

2

):

List

append

string

j

j

+

3

])

if

string

i

i

+

3

in

List

return

False

List

=

[]

return

True

while

True

try

string1

=

input

()

if

length

string1

and

symbol

string1

and

repeatedChildString

string1

):

print

‘OK’

else

print

‘NG’

except

break

標簽: String  str  re  compile  print