Python 基礎 字符串拼接 + if while for循環

註釋
單行註釋 #
多行註釋 ''' 三個單引號或者三個雙引號 """

''' 用三引號引住能夠多行賦值

用戶交互 input

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

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

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

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


#"".format()this

字符串格式化 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
#     對數字有效,對於2、8、十六進制,會對應顯示 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 使用
例:spa

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
例:code

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 跳出當前循環,進入下次循環orm

做業blog

編寫登錄接口索引

  • 輸入用戶名密碼
  • 認證成功後顯示歡迎信息
  • 輸錯三次後鎖定
 
 
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循環遍歷
相關文章
相關標籤/搜索