python字符串

python字符串相关

切片

name = "baoqiang"

print(name[0:6:2])

结果:

boi

切片用例

name = "abcefg"
name[-1] = g
name[-2] = f
name[3:6] = efg
name[3:-1] = ef
name[3:] = efg
name[-1:-3] = ''
name[3:-1] = ef
name[-1:-3:-1] = gf
name[-1:-5:-1] = gfec
name[-1:-6:-1] = gfecb
name[-1:-7:-1] = gfecba
name[-1::-1] = gfecba
name[::-1] = gfecba

注:以上结果并没有改变字符串本身

例子

name = "baoqiang"

print(name[0:6:2])

print(name.count("a"))

print(name.replace("a", "AA", 1))

print(name.replace("a", "AA"))

str1 = " haha heihei hehehe"
print(str1.split(" "))

print(str1.split(" ", maxsplit=1))

print(name.capitalize())

结果

boi
2
bAAoqiang
bAAoqiAAng
['', 'haha', 'heihei', 'hehehe']
['', 'haha heihei hehehe']
Baoqiang
  • 列表循环例子
#需求文档:显示所有文件的后缀

# 1. 先列举一下常用文件的后缀
fileNames = ["01.py", "02.txt", "03.rar", "04.c", "05.cpp", "06.php", "07.java", "index.html", "finally.doc"]

# 2. 思路

# 2.1 先把01.py文件的后缀显示出来。

tempName = fileNames[0]

position = tempName.rfind(".")

print(tempName[position:])

# 2.2 把上一步整体放入循环中,即可完成

for tempName in fileNames:

    position = tempName.rfind(".")

    print(tempName[position:])
print("--------------华丽的分割线--------------")

i = 0
length = len(fileNames)
while i < length:
    tempName = fileNames[i]
    position = tempName.rfind(".")
    print(tempName[position:])
    i += 1

列表删除操作:

list.pop() 删除最后一个元素

del list[n] 按照下标进行删除

list.remove(“a”) 按照值来进行删除

列标嵌套办公室例子:

一个学校,有三个办公室,现在有8位老师等待工作的分配,请编写程序,完成随机的分配

import random

#1.先定义一个列表,用来存储8位老师的名字
teachers = ["A君1", "B君2", "A君3", "A君4", "5", "6", "7", "8"]

#2.定义一个列表,有三个空的办公室,用来等待其他老师加入

offices = [[], [], []]

#3.通过循环的方式,将8位老师随机分配到3个办公室中
#注意:所谓的随机分配,就是获取一个随机的办公室号,然后把这个老师添加到里面即可

for name in teachers:
    index = random.randint(0,2)
    offices[index].append(name)

# print(offices)
#输出每个办公室的老师信息
# for name in teachers:
#     for subName in name:
#         print(subName)
i = 1
for room in offices:
    #print(room)
    print("办公室:" + str(i))
    for teacherName in room:
        print(teacherName)
    print("-"*20)
    i += 1

运行结果:

D:\1a\python3.6.1\python.exe E:/python_workspace/python_notes/example6-office.py
办公室:1
A君3
A君4
7
--------------------
办公室:2
5
8
--------------------
办公室:3
A君1
B君2
6
--------------------
Process finished with exit code 0

列标嵌套办公室例子:

新功能添加,规定每个办公室至少有两名老师

参见网址https://www.zhihu.com/question/67512952

作者鲁智深
链接https://www.zhihu.com/question/67512952/answer/266149550
来源知乎
著作权归作者所有商业转载请联系作者获得授权非商业转载请注明出处

#coding=utf-8
import random

#办公室
office = [[],[],[]]

#有多少个老师
teachar = ['a','b','c','d','e','f','g','h']
i = 0

last_index = len(teachar)#老师的个数

while i < last_index-1:
    index = random.randint(0,2)
    name = teachar[i]
    office[index].append(name)
    i += 1

#最后一个老师
teacher_last = teachar[last_index -1]

if not office[0]:
    office[0].append(teacher_last)
elif not office[1]:
    office[1].append(teacher_last)
elif not office[2]:
    office[2].append(teacher_last)
else:
    office[index].append(teacher_last)#最后一个老师随机给办公室
i = 1
for new_name in office:
    print("办公室%d人数为: %d" %(i,len(new_name)))
    i += 1
    for t_name in new_name:
        print(t_name ,end="")
    print("")

运行结果:

img

遍历字典

enumerate() 枚举

chars = ['a', 'b', 'c', 'd']
for i, ch in enumerate(chars):
  print i, chr

运行结果:

0 a
1 b
2 c
3 d

字符串匹配

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦