码迷,mamicode.com
首页 > 编程语言 > 详细

python 基础

时间:2019-05-24 00:49:30      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:class   循环   title   bre   格式化   while   字符   swap   长度   

用户输入用户名及密码,判断是否正确

i = 3
username = "chengxi"
password = "123456"
while i >0:
    name = input ("请输入你的用户名:")
    i -= 1
    if name == username:
        passwd = input ("请输入你的密码:")
        if passwd == password:
            print ("登录成功")
            break
        else:
            if i == 0:
                print ("你机会用完")
                break
            print ("密码错误")
            print ("你还有%s次机会"%(i))
            continue
    else:
        print ("用户名错误!请重新输入")
        print ("你还有%s次机会"%(i))
测试
请输入你的用户名:chengxi
请输入你的密码:123456
登录成功

  字符串的输出格式 首字母大写

s = ‘alexWUsir‘
s1 = s.capitalize() #首字母大写
print (s1)
测试
Alexwusir

  输入验证,忽略大小写

s_str=‘acEQ‘
you_input = input(‘请输入验证码,不区分大小写‘)
if s_str.upper() == you_input.upper():
    print (‘输入成功‘)
else:
    print (‘请重新输入‘)
测试
请输入验证码,不区分大小写aceq
输入成功

  大小翻转

s = ‘chENxIAi‘
s3 = s.swapcase()
print (s3)
测试
C:\Users\zrd\AppData\Local\Programs\Python\Python37\python.exe G:/python/v/rt.py
CHenXiaI

  单词首字母大写

s = ‘chenxi danniel‘
s4 = s.title()
print(s4)
测试
Chenxi Danniel

  设置打印字符串的总长度,并且打印居中字符,以空格填充

s = ‘chenxi AI ni‘
s5 = s.center(20)
print (s5)
测试
    chenxi AI ni    

  设置字符总长度,并且设置填充物

s = ‘chenxi AI ni‘
s5 = s.center(20,‘*‘)
print (s5)
测试
****chenxi AI ni****

  测量字符串数组等长度数字

s = ‘hhkjjlhchhtfdrdtvgxf‘
l = len(s)
print (l)
测试
20

  判断字符是否以ch 开头

s = ‘chenxi‘
s7 = s.startswith(‘ch‘)
print (s7)
测试
True

  判断第三个字符是否是n

s = ‘chenxi‘
s7 = s.startswith(‘n‘,3)
print (s7)
测试

True

  寻找这个元素最后返回的是这个元素下标

s = ‘chenxi‘
s7 = s.find(‘n‘)
print (s7)
测试
3

  另一种场景

s = ‘chenxi‘
s7 = s.find(‘en‘)
print (s7)
测试
2

  找不到的场景

s = ‘chenxi‘
s7 = s.find(‘Y‘)
print (s7)
测试
-1

  while循环1打印值10

i=1
while i < 11:

       print(i)
       i=i+1
测试
1
2
3
4
5
6
7
8
9
10

  计算从1+2+3.....+50000的和

count = 1
sun = 0
while count <= 50000:
       sun = sun + count
       count = count + 1
print(sun)
测试
1250025000

  用户输入什么打印什么,输入指定的字符退出程序

while True:
       cont = input("输入内容(x)")
       if cont == ‘cx‘:
              exit(0)
       print(cont)
测试
输入内容(cx)hjk
hjk
输入内容(cx)ggh
ggh
输入内容(cx)cx
Process finished with exit code 0

  用户输入什么打印什么,输入指定字符打印特定字符退出

while True:
       cont = input("输入内容(x)")
       if cont == ‘cx‘:
              break      #跳出本层循环
       print(cont)
print(‘kj‘)
测试
输入内容(x)hgvfhv
hgvfhv
输入内容(x)cx
kj

  用户输入什么打印,输入特定字符后不打印,直接让重新用户输入;死循环

while True:
       cont = input("输入内容(x)")
       if cont == ‘cx‘:
              continue     #终止本次循环,直接开始下一次循环
       print(cont)
print(‘kj‘)

测试

输入内容(x)gjg
gjg
输入内容(x)cx
输入内容(x)yuhkjl
yuhkjl
输入内容(x)\ddtfg
\ddtfg
输入内容(x)cx
输入内容(x)

  格式化输出

s = "阅读使我快乐"
print(s)
p = "钞票使我安全"
print(p)
 测试
C:\Users\zrd\AppData\Local\Programs\Python\Python37\python.exe D:/python/test.py
阅读使我快乐
钞票使我安全

  格式

s = "阅读使我快乐"
print(s)
p = "钞票使我安全"
print(p)
x = "日天"
swe = "牛B"
print(x)
print(swe)
s2 = swe+"是一个"+x+"的"
print(s2)

测试

阅读使我快乐
钞票使我安全
日天
牛B
牛B是一个日天的

  格式化输出 %s占位符

name = "gaojihui"
xia = "yuchpiu"
s = "%s是一个%s的人" %(name,xia)
print(s)
测试
gaojihui是一个yuchpiu的人

  格式化输出

name = "chenxi"
age = "19"
hobby = "配齐"
print("我叫%s,我喜欢%s,我今年%s岁了" % (name,hobby,age))
测试
C:\Users\zrd\AppData\Local\Programs\Python\Python37\python.exe D:/python/test.py
我叫chenxi,我喜欢配齐,我今年19岁了

Process finished with exit code 0

  格式化输出

name = "chenxi"
age = 19
hobby = "配齐"
print("我叫%s,我喜欢%s,我今年%d岁了" % (name,hobby,age))   #%d数字的占位符,%s表示所有字符占位符
测试
C:\Users\zrd\AppData\Local\Programs\Python\Python37\python.exe D:/python/test.py
我叫chenxi,我喜欢配齐,我今年19岁了

Process finished with exit code 0

  格式化输出%

name = "晨曦"
age = 20
print("我叫%s,我今年%d,我学习python%%15" % (name,age))   #注意当字符串有占位符,要表示百分比是%%号,否则报错
测试
C:\Users\zrd\AppData\Local\Programs\Python\Python37\python.exe D:/python/test.py
我叫晨曦,我今年20,我学习python%15

  

 

python 基础

标签:class   循环   title   bre   格式化   while   字符   swap   长度   

原文地址:https://www.cnblogs.com/rdchenxi/p/10873672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!