while,字符串练习

# -*- coding:utf-8 -*-

# 输出1--100的偶数

i = 1
while i < 101:
    if not i//2 == 0:
        print(i)
        i +=1
    i +=1

# 求1--99的和
i = 1
e = 0
while i < 100:
    e += i
    i += 1
print(e)

# 求1--100所有奇数的和
i = 1
q = 0
while i < 101:
    if not i%2 == 0:
        q += i
        i += 1
    i+=1
print(q)


# 用户三次机会
i = 0
while i < 3:
    y = input("账号")
    m = int(input("密码"))
    s = 123
    if y == s and m == s:
        print("登录成功")
        break
    else:
        print("密码错误还剩%d"%(2-i))
        i += 1


# 字符串练习****************************************************

s = "qwrw\terqwe\trqerq\twerqw"
print(s.expandtabs(0)) # 设置字表符显示几位

s = "qweqweqweafafafa"
print(s.capitalize())  # 设置首字母大写

s = "QWDDDDDDeqweqweqwe"
print(s.casefold())   # 设置大写变小写
print(s.lower())      #casefold更厉害

s = "WQWEqweq wreqwe"
print(s.center(20,"中")) #设置宽度,居中,两边用符号填充,默认空格

s = 'qwqeqweqw'
print(s.count("qw",0,3))  # 使用切片形式来寻找指定字符串出现次数,返回次数

s = "w3eqwqweq"
print(s.encode("utf-8","w")) # 返回utf-8格式,将unicode转换为utf-8

s = "ewwewe"
print(s.encode().decode()) # decode的作用是将其他编码转换成Unicode编码

s = "weqweq"
print(s.endswith("q"))  # 以什么结尾,正确返回True,错误返回False
print(s.startswith("q"))  # 以什么开头,正确返回True,错误返回False

s = "wrwrwerwer"
print(s.find("wr"))  # 从开始找返回字符串所在位置,未找到返回 -1

s = "wrwerqwr"
print(s.index("q"))   # 从头找返回字符串所在位置, 未找到报错

猜你喜欢

转载自blog.csdn.net/weixin_42100915/article/details/80175578