python循环字符串拼接_Python 自学的日子--One day笔记- 字符串拼接 + if while for循环...

注释

单行注释 #

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

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

用户交互 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()

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 = "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‘)

字符串基础

(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循环遍历

原文:http://www.cnblogs.com/Upward-man/p/5754293.html

你可能感兴趣的:(python循环字符串拼接)