python学习day02

字符串处理
 

str()   字符串
int()   整数
long()  长整形
float() 浮点数
s="songajiwei"
print "\'\"hanxiaogang\"\'"  ##打印出'"引号
输出:'"hanxiaogang"'
s           ###打印s
输出: 'hanxiaogang'
s[0]            ###获取第一个字符
输出: 'h'     
s[-1]           ###获取最后一个字符
输出: 'g'
s[0:5]          ###获取1-5的字符(切片)
输出: 'hanxi'
s[0:-2]         ###获取1-倒数第2个的字符
输出: 'hanxiaoga'
s[0:5:2]        ###每隔1个字符获取1-5个字符
输出: 'hni'
s[:]            ###打印全部
输出: 'hanxiaogang'
s[::-1]         ###倒序打印全部
输出: 'gnagoaixnah'
"_" in s        ###判断是否有_
输出: True
'*'*10          ###转义*打印出10个*
输出: '**********'
'*'*10+"hanxiaogang"+'*'*10
输出: '**********hanxiaogang**********'
len(s)
输出: 11

2.变量名合法性判断
变量名合法规则
1.变量名由字母,数字或者下划线组成
2.变量名只能以字母或下划线开头
题目:判断变量名是否合法

while True:
    s = raw_input("变量名:")
    if s[0].isalpha() or s[0] == "_":
        for i in s[1:]:
            if i.isalnum() or i == "_":
                continue
            else:
                print "%s 变量名不合法!Error: 变量名后面的字符" % (s)
                break
        else:
            print "%s变量名合法" % (s)
    else:
        print "%s 变量名不合法! Error: 变量名第一个字符" % (s)

其他用法

a = "fadsj.log"
a.endswith  #多用于查找指定的文件格式(.log, .png.....)
a.startswith    #多用于判断使用的协议(http://, https://, ftp://)
a.strip     #去掉所有空格
a.lstrip    #去掉左边的
a.rstrip    #去掉右边的
a.replace(" ","")   #替换
a.center    #居中
a.ljust     #左
a.rjust     #右
a.split     #把字符串分割成字符串数组
join    #把字符以" "的形式连接起来
#内置函数:
            cmp, max, min, enumerate, zip, sum, len, abs

3.输入两个字符串,A字符串不能包含B字符串带的字符

while True:
    a = raw_input("字符串A:")
    b = raw_input("字符串B:")
    for i in b:
        a=a.replace(i,"")
    else:
        print a

4.输入hello xiaomi 输出xiaomihello

print (((((str((raw_input("请输入一个句子:").split()[::-1]))).replace(",", "")).replace("'", "")).replace("[", "")).replace("]",""))
或者
print " ".join(raw_input("请输入一个句子:").split()[::-1])   ##把字符以" "的形式连接起来

5.枚举

a = raw_input("请输入:")
b = len(a)
for i in range(0,b):
        print i,a[i]

效果:
 0     h
 1     e
 2     l
 3     l
 4     o
 s = "hello"     

方法二

for i,j in enumerate(hello):
    print i,j

6.输入字符串,分别统计字母,数字,空格,特殊符号的个数

while True:
    s = raw_input("输入字符串:")
    count = len(s)
    a,b,c,d=0,0,0,0
    for i in s:
        if i.isalpha():
            a += 1
        elif i.isdigit():
                b += 1
        elif i.isspace():
                c += 1
        else:
                d += 1
    print "字母个数:%d\n数字个数:%d\n空格个数:%d\n其他字符个数:%d\n"%(a,b,c,d)

7.列表

array = [1,2,3,4]
list = [1,1.0,1L,"hello",1+3j]

#索引
print list[0], list[1]
#切片
print list[::-1]
#重复
print list * 3
#连接
print array + list
#成员操作符
print 1 in li
print 1 not in li

列表的增删改查

allow_ip = ['172.25.254.91', '172.25.254.2', '172.25.254.14', '172.25.254.32']
# 增
# append追加元素到列表的最后;
allow_ip.append('172.25.254.6')
print allow_ip

# insert将元素添加到指定索引的前面;
allow_ip.insert(1,'172.25.254.34')
print allow_ip

# extend是追加多个元素到列表中;
allow1_ip = ['172.25.254.56', '172.25.254.66']
allow_ip.extend(allow1_ip)
print allow_ip

# 改
# 给列表的指定索引重新赋值;
allow_ip[0] = '172.25.254.11'
print allow_ip

# 查
# 显示指定元素出现的次数;
 print allow_ip.count('172.25.254.1')

# 显示指定元素的索引值;如果出现多个,显示小的那个索引;如果元素不存在,报错,ValueError;
 print allow_ip.index('172.25.254.1')

# 删

# pop是删除指定索引的值;如果列表为空或者索引不在范围内,则报错; 如果不指定索引值,默认删除最后一个元素;
allow_ip.pop()

# 删除列表中最先出现的值,不存在,则报错;
allow_ip.remove('172.25.254.1')
print allow_ip

# 反转列表
allow_ip.reverse()

# 列表排序
allow_ip.sort()
print allow_ip

题目: 1). 已知多个用户名和密码;
2). 判断用户名是否存在,
如果登陆的用户不存在,则报错, 清注册用户;
如果用户存在, 则判断密码是否正确:
如果正确, 输出用户登陆成功;
如果不正确, 输出登陆失败;

if username not in users:
        print "%s 用户不存在,请注册用户!" %(username)
        # useradd = input("要注册的用户名:")
        # users.append(useradd)
        # passadd = input("输入用户名密码:")
        # passwd.append(passadd)
else:
    password = raw_input("密码:")
    a = users.index(username)
    while True:            
            if password == passwd[a]:
                    print "%s用户登陆成功!" % (username)
                    print  """
                                    ATM管理系统

                            1. 取款
                            2. 存款
                            3. 查询余额
                            4. 退出
                        """
                    choice = input("请输入你的选择:")
                    if choice == 1:
                        pass
                    elif choice == 2:
                        pass
                    elif choice == 3:
                        pass
                    elif choice == 4:
                        exit(0)
                    else:
                        print "请输入正确的选择!"
            else:
                print "密码输入错误!"

题目:批量生成学号
学号前4位为1705
依次有三个学院:电子工程学院(01), 软件学院(02), 商学院(03)
每个学院有200人;
170501001

for i in  ['01', '02', '03']:
    for j in range(1,201):
        print "1705%s%.3d" %(i,j)

猜你喜欢

转载自blog.csdn.net/qq_41636653/article/details/82623985