[PYTHON] 字符串string

今天记录一些简单实用的str methods(按照自己收集的)

看一下方法调出(Method call)的两种方法

1.str methods中的is类:#is类返回的结果为 True和False

#以上返回的结果都为True

2.字符串的基本处理:

3.str.formart()#在前文已经介绍过的 [PYTHON]格式化方法具体有介绍,请查阅(●'◡'●)

4.methodsd的重复使用:

重复使用的方法有很多种,根据要求自己设计o(* ̄▽ ̄*)ブ

5.利用str.maketrans()制作随机密码:

 

#制作随机密码还可以用random模块,有机会为大家介绍

6.最后给大家附上一道简单的应用题:

Write a program that asks the user to enter a password and verify if the password is strong, normal, or weak.
Assume that the user only enters alphabets and/or digits (no symbols or blanks will be entered).
Hint: Use str.isdigit() and str.isalpha(). Use help() to find out how to use them.
1) Weak password: The length of password is less than 8 characters, or the password consists of digits only.
1)密码弱:密码长度小于8个字符,或者密码仅由数字组成。
2) Normal password: The length of password is greater than or equal to 8 characters, and the password consists of alphabets only.
2)普通密码:密码长度大于或等于8个字符,密码仅由字母表组成。
3) Strong password: The length of password is greater than or equal to 8 characters, and the password consists of both alphabets and digits.
3)强密码:密码的长度大于或等于8个字符,密码由字母表和数字组成。

 

 

答案:

password=input('Enter a password:')
if len(password) <8 or password.isdigit()==True:
print(password,'is a weak password.')
elif len(password) >= 8 and password.isalpha() == True:
print(password, 'is a normal password.')
elif len(password) >=8 and password.isalpha()==False and password.isdigit()==False:
# password.isalnum() == True判断是否为数字字母混合
print(password,'is a strong password.')
______________________________________________________________________________________
str methods的方法有很多,我只是把一部分介绍给大家
具体的请查找PYTHON标准库
4.7.1 string methods:https://docs.python.org/3.5/library/stdtypes.html#string-methods

 

 

posted @ 2017-10-27 15:47 HONG-LIANG 阅读(...) 评论(...) 编辑 收藏