python-03.字符串列表字典

本文介绍了Python中的字符串基础知识,包括字符串的定义、转换方法、输出输入方式,以及字符串的下标、切片、逆序等操作。此外,还详细讲解了字符串的常见操作,如find、index、count、replace、split等。接着,文章转向列表的介绍,讨论了列表的定义、增删改查方法,并提到了列表与C语言数组的区别。最后,文章涉及了字典的使用,包括字典的增删改查操作及其在名片管理系统中的应用,并讲解了遍历字典的方法。

字符串

什么叫字符串?
num = 100
num2 = “100”
第二个叫做字符串
数字100占一个字节,"100"字符串占三个字节。

int(x[,base]) 将x转换为一个整数
str(x) 对象x转化为字符串
举例

num = "100"
print(int(num))

结果
100

num = 100
a = str(num)
print(len(a))

结果
3

字符串的输入和输出

name = "laowang"

print("姓名:%s"%name)

结果

姓名:laowang

字符串用%s,整数用%d

组合:
a = “lao”
b= “wang”
c= a + b
c= “laowang”

A = 100
B = 200
C = A+B
C = 300

字符串的下标:

name = "abcdef"
print(name[0])

结果
a

下标是从0开始,而且输出时,下标不要越界,如上题,最大只能输出name[5]

name = "abcdef"

name[-1]
f
name[-2]
e

-1表示取倒数第一个
切片

name = "abcdef"
name[2:5]
cde

name[2:-2]
cd

name[2:]
cdef

[2:5] 从第三个开始取,取到第六个,但是不包含第六个,只取到前一个。

name = "abcdefABCDEF"
print(name[2:-1:2])

结果

ceACE

name[2:1:2]表示从第三个取到倒数第一个前一个,然后最后的2表示步长(默认是1)。

逆序

name = "abcdefABCDEF"
print(name[-1::-1])

结果

FEDCBAfedcba

也可以

name = "abcdefABCDEF"
print(name[::-1])

结果

FEDCBAfedcba

字符串的常见操作

find与index

mystr = "hello world itcast and itcastsssscpp"

mystr.find("world")
6

mystr.index("world")
6

mystr.find("dongge")
-1

mystr.index("dongge")
产生异常

mystr.find("itcast")
12

mystr.rfind("itcast")
23

mystr.rfind("itcast")
23

index与find之间唯一区别就是查找不存在的字符输出的结果
输出的数字表示该字符串第一个字母所在位置。

count用来统计个数

mystr.count("world")
1

mystr.count("itcast")
2

mystr.count("dongge")
0

replace

mystr = "hello world itcast and itcastsssscpp"

mystr.replace("itcast", "xxx", 1)#不设置1参数,那么会挨个替换
hello world xxx and itcastsssscpp

split切割

mystr = "hello world itcast and itcastxxxcpp"
a = mystr.split(" ")
print(a)

结果

['hello', 'world', 'itcast', 'and', 'itcastxxxcpp']

capitalize

mystr = "hello world itcast and itcastxxxcpp"
a = mystr.capitalize()
print(a)

结果

Hello world itcast and itcastxxxcpp

title

mystr = "hello world itcast and itcastxxxcpp"
a = mystr.title()
print(a)

结果

Hello World Itcast And Itcastxxxcpp

startswith 与 endswith
确定开头和结尾对不对

name = "wang xxxxx"
print(name.startswith("wang"))
True
file_name = "xxx.txt"
print(file_name.endswith(".txt"))
True

lower 和 upper
将字符串所有字母变小写和大写

ljust rjust center
将字符串左对齐,右对齐,居中

lstrip rstrip strip
去左边空格,去右边空格,去左右空格

partition

mystr = "hello world itcast and itcastxxxcpp"
a = mystr.partition("itcast")
print(a)

结果
('hello world ', 'itcast', ' and itcastxxxcpp')

结果是元组

splitlines

mystr = "hello\nworld\nitcast\nand\nitcastxxxcpp"
a = mystr.splitlines()                           
print(a)

结果
['hello', 'world', 'itcast', 'and', 'itcastxxxcpp']                                

按行切割,组成列表

isalnum

num = "1q"
num.isalnum()
True

num = "1"
num.isdigit()
True

num = "q"
num.isalpha()
True

拓展 .isspace() 意思就是全是空格才会显示True

join()

a = ["aaa", "bbb" ,"ccc"]
b = "="
b.join(a)
aaa=bbb=ccc

.split()
split()可以直接去掉字符串中的空格\t等等

列表的引入、定义、和C语言的数组不同的点
列表:[],
存储不同的变量
变量的类型可以是整型、字符串的组合,而C语言是统一的

增删改查
添加
names.append()#添加到原有列表的最后
names.insert(位置,要添加的内容)#按位置添加
names.extend(name2)#将两个列表合并

删除
names.pop()#把列表最后一个拿出来删掉
names.remove()#选择一个元素删除,只删一次,从左开始
del names[0]#根据下标删

修改
names[0] = "新值“

查询
if xxx in
if xxx not in

名字管理系统

#1. 打印功能提示
print("="*50)
print("请输入对应序号,进入对应功能")
print("1.增加一个姓名")
print("2.删除一个姓名")
print("3.查找一个姓名")
print("4.更改一个人的姓名")
print("5.退出")
print("="*50)

names=[]#定义一个空的字典

while True:
    #2.获取用户的输入
    number = int(input("请输入序号:"))
    #3.根据用户的数据执行相应的功能
    if number==1:
        add_name=input("请输入要增加的姓名:")
        names.append(add_name)
        print(names)
    elif number==2:
        remove_name=input("请输入要删除的名字:")
        if remove_name in names:
            names.remove(remove_name)
        else:
            print("没有这个人")
        print(names)
    elif number==3:
        find_name=input("请输入要查找的名字:")
        if find_name in names:
            print("有这个人")
        else:
            print("查无此人")
    elif number==4:
        replace_name=input("请输入要替换的人的姓名:")
        new_name=input("请输入替换之后的人的名字:")
        if replace_name in names:
            names[names.index(replace_name)]=new_name
        else:
            print("没有这个人")
        print(names)
    elif number==5:
        break
    else:
        print("你的输入有误!请重新输入!")

11.字典的引入和定义
infor = {键:值,键:值}
eg
infor = {“name”:“班长”,“addr”:“山东”,“age”:18}
print("%s %s %d"%(infor[name],infor[addr],infor[age]))

13.字典的增删改查
infor = {“name”:“dongge”}
①增
infor[“age”]=18
{“name”:“dongge”,“age”:18}
②改
infor[“age”]=19
{“name”:“dongge”,“age”:19}
③删
del infor[“age”]
如果没有程序就会崩
④查
infor[“age”]
19
#如果没有的话程序会崩
infor.get[”age“]
19
#如果没有的话不返回任何值,不会崩
12.名片管理系统

#1.打印功能提示
print("="*50)
print("名片管理系统")
print("1.添加一个新的名片")
print("2.删除一个名片")
print("3.修改一个名片")
print("4.查询一个名片")
print("5.显示所有名片")
print("6.退出系统")
print("="*50)

card_infors =[]
while True:
    #2.获取用户的输入
    number =int(input("请输入操作序号:"))
    #3.根据用户的输入执行相应的功能
    if number==1:
        new_name = input("请输入姓名:")
        new_qq = input("请输入QQ:")
        new_weixin = input("请输入微信:")
        new_addr = input("请输入住址:")

        new_infors={}
        new_infors["name"]=new_name
        new_infors["qq"]=new_qq
        new_infors["weixin"]=new_weixin
        new_infors["addr"]=new_addr

        card_infors.append(new_infors)

        #print(card_infors)#for test

    elif number==2:
        pass
    elif number==3:
        pass
    elif number==4:
        find_name=input("请输入要查找的名字:")
        find_flag=0
        for temp in card_infors:
            if temp["name"]==find_name:
                print("%s\t%s\t%s\t%s"%(temp["name"],temp["qq"],temp["weixin"],temp["addr"]))
                find_flag=1
                break
        if find_flag==0:
                print("查无此人")
    elif number==5:
        print("姓名 QQ 微信 住址")
        for tempall in card_infors:
            print("%s\t%s\t%s\t%s" % (tempall["name"], tempall["qq"], tempall["weixin"], tempall["addr"]))
    elif number == 6:
        break
    else:
        print("输入错误!请重新输入")

14.for 和 while 的遍历
nums = [11,22,33,44,55]

nums_length = len(nums)
i=0
while i<nums_length:
	print(nums[i])
	i+=1
	
for num in nums:
		print(num)

15.for循环中的else

card_infors = [{"name":"laowang","age":18},{"name":"laoli","age":19},{"name":"laozhao","age":20}]

find_name = input("请输入要查询的名字:")
for temp in card_infors:
	if temp["name"]==find_name:
		print("找到了。。。")
		break
else:
	print("没有找到")

可以和上面名片管理中的标记法对比!

16.列表中的append和extend

a = [11,22,33]
b = [44,55]

a.append(b)
 [11,22,33,[44,55]]

a.extend(b)
 [11,22,33,44,55]

append的一个注意点:
a = a.append(b)
print(a)
None

因为a.append(b)的结果不是一个值,所以输出是None

18.字典的常见操作、遍历
infor={“age”:18,“name”:“laowang”}
infor.keys()#得到所有的键
infor.values()#得到所有的值
infor.items()#得到每个键值(元组形式)

#拆包
a = (11,22)
c,d=a
c=11
d=22

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值