注释
单行注释 #
多行注释 ''' 三个单引号或者三个双引号 """

''' 用三引号引住可以多行赋值

用户交互 input

字符串拼接
+  ""%() "".format()推荐使用
name = input("name:")
age = int(input("age:"))
sex = input("sex:")
例:+
# 字符串拼接+

info1 = '''----info in ''' + name + '''---
name:''' + name + '''
age:''' + age + '''
sex:''' + sex + '''
'''

例:""%()
# %格式化字符串

info = '''------info in %s -------
name:%s
age:%d
sex:%s
''' % ("name", "name", age, "sex")

#"".format()

字符串格式化 format    []都是可选的,可填可不填
格式:[[fill][align][sign][#][0][width][,][.precision][type]
fill  填充字符      
align 对齐方式        
    ^ 居中            s = "{:-^20d}".format(20)            -----20-----
    < 内容左对齐    s = "{:-<20d}".format(20)            20----------
    > 内容右对齐    s = "{:->20d}".format(20)            ----------20
    = 内容右对齐,将符号放在填充字符的左侧,只对数字有效
sign  有无符号数字(感觉用处不大)
    +       正数加+ 负数加-        s = "{:+d} this is Numbers".format(-20)        -20
    -        正数不变  负数加-    s = "{:-d} this is numbers".format(23)         23
    空格    正数空格   负数加-  s = "{: d} this is numbers".format(23)      23
#     对数字有效,对于二、八、十六进制,会对应显示 0b 0o 0x    s = "{:#0x}".format(213)
width 格式化字符宽度            s = "{:-^20d}".format(20)
,     对大的数字有效,添加分隔符 如1,000,000    s = "{:,d}".format(2000000000)   2,000,000,000
.precision  小数位保留精度    s = "{:.2f}".format(12.2323)        12.23
type  格式化类型
    s 字符串     s = "this is {}".format("string")
    b 十进制转二进制表示 然后格式化   s = "{:d}".format(23)   10111
    d 十进制
    o 十进制转八进制表示 然后格式化        s = "{:o}".format(23)   27
    x 十进制转十六进制表示 然后格式化   s = "{:x}".format(23)   17
    f 浮点型 默认小数点保留6位
    % 显示百分比 默认小数点后6位         s = "{:.2%}".format(0.1234) 参数可用[]及{} ,使用时必须加*,**
s = "my name is {},age is {}".format(*["niu", 25]) s = "my name is {name}, age is {age}".format(**{"name": "niu", "age": 25})
info3 = '''---info in {_name}---
name:{_name}
age:{_age}
sex:{_sex}
'''.format(_name=name,
_age=age,
_sex=sex)
info4 = '''---info in {0}---
name:{0}
age:{1}
sex:{2}'''.format(name, age, sex)

模块定义:
密文密码:getpass  引用后使用,getpass.getpass()
if else 使用
例:

username = "username"
password = ""
_Username = input("Username:")
_Passwd = input("Password:")
if username == _Username and password == _Passwd:
print("welcome user {name} to beij".format(name=username))
else:
print("Invalid username or passwd")

if elif else
例:

Myage = 37
InputAge = int(input("please input my age:"))
if InputAge == Myage:
print("It's right")
elif InputAge > Myage:
print("Think small")
else:
print("Think big") While else 循环
count = 0
while count < 3:
Myage = 37
InputAge = int(input("please input my age:"))
if InputAge == Myage:
print("It's right")
break
elif InputAge > Myage:
print("Think small")
else:
print("Think big")
count+=1
else:
print("fuck you!")

break    跳出当前整个循环
continue 跳出当前循环,进入下次循环

作业

编写登陆接口

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定

old_uname = open(r'C:\Users\Administrator\Desktop\username.txt', 'r').readlines()
count = 0
while count < 3:
username = input("please your username:")
passwd = input("please your passwd:")
for i in old_uname:
if i == username:
print("wolcome to your blogs:{_uname}".format(_uneme=username))
break
else:
continue
else:
count += 1
if count == 3:
continue
print("The password you entered is incorrect!please input again...")
else:
print("三次错误,账号已锁定")
open(r'C:\Users\Administrator\Desktop\lockname.txt', 'a').write(username + '\n')

字符串基础
#字符串操作
#去掉空格 strip () 括号内可指定 默认是空格
username = input("username:")
if username.strip('-') == "zhang":
print(username.strip('-'))
print("welcome %s to beijing" % username)

# 分割 split 可指定根据什么分割
list11 = "welcome to beijing"
list2 = list11.split()
print(list2) # ['welcome', 'to', 'beijing']

# 合并 join 可指定根据什么合并
list3 = ":".join(list2) + '\n'  # welcome:to:beijing
list4 = " ".join(list2) # welcome to beijing
print(list3, list4)
# 判断有没有空格\切片
name = "mr,niu"
print("," in name)
name1 = "mr niu"
print(" " in name1)
print(name[2:4])

# format 字符串格式化
men = "my name is {name}, age is {age}"
all = men.format(name="niu", age=23) men1 = "my name is {0}, age is {1}"
all1 = men1.format("niu", 23)
print(all1) fill = "niu"
fill.isdigit() # 判断是不是数字
fill.endswith('') # 判断是不是以指定字符串结尾
fill.startswith('') # 判断是不是以指定字符串开头
fill.upper()   # 批量转换成大写
fill.lower() # 批量转换成小写
fill.capitalize() # 第一个字母大写,其他都小写
fill.title() # 每个单词的首字母大写,其他都小写
print(fill.center(20, '='))
(1)、转义字符串             
(2)、raw字符串--转义机制  open(r'c:\tmp\a.txt','a+')
(3)、Unicode字符串
(4)、格式化字符串  "age %d,sex %s,record %m.nf"%(20,"man",73.45)
字符串基础操作
 + 连接 、* 重复、s[i] 索引(index)、s[i:j] 切片(slice)、for循环遍历

最新文章

  1. 泛型数组列表 ArrayList
  2. css3过渡
  3. jquery ajax get /post
  4. 修改mac host
  5. zedboard 构建嵌入式linux
  6. android sqlite支持的数据类型
  7. POJ1751--Highways(最小生成树,kauskal)
  8. CentOS 6.7下iPython提示“WARNING: Readline services not available or not loaded.”的解决办法
  9. dispatch_async 与 dispatch_get_global_queue 的使用方法
  10. vs2003的代码考到vs2010 会出现(Windows CR LF)
  11. 使用原生JDBC循环读取文件并持久化到数据库
  12. [PHP]算法-二进制中1的个数的PHP实现
  13. js中得计算问题算式结果拼接成字符串怎么解决
  14. 人群密度估计 CrowdCount
  15. sql server 作业收缩数据库
  16. 【Python】etree方法生成,解析xml
  17. django之ORM专项训练之图书信息系统 了不起的双下方法实战 和 分组 聚合 Q, F查询,有約束和無約束
  18. 【IIS错误 - HTTP 错误 500.19】HTTP 错误 500.19- Internal Server Error 错误解决方法(一)
  19. ionic3 下创建ionic1项目
  20. JavaScript 检查是否是数字

热门文章

  1. redhat开启linux server
  2. myEclipse和eclipse修改或复制项目名称后-更新部署名称
  3. Let&#39;s Encrypt+Apache+Tomcat实现免费HTTPS
  4. Java中的Stringbuffer类解析
  5. java 反射(reflect)总结,附对象打印工具类
  6. hibernate关联关系映射详解
  7. 【转】SVN:Android Studio设置忽略文件
  8. boost库在工作(37)网络UDP服务端之七
  9. 基于VMware为CentOS 6.5配置两个网卡
  10. Day05 - Python 常用模块