设为首页 加入收藏

TOP

Python 基础 字符串拼接 + if while for循环(二)
2017-09-30 17:19:09 】 浏览:2988
Tags:Python 基础 字符串 拼接 while for 循环
x:{2}'''.format(name, age, sex)

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

username = "username"
password = "123456"
_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 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python-Day3 Python基础进阶之集.. 下一篇Python Base Four

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目