Python基础学习之字典和字符串

目录

一、字典

1.字典的基本介绍

2.创建字典

3.keys()、values()和 items()方法

4.检查字典中是否存在键或值

5.get()方法

6.setdefault()方法

7.其他方法

二、字符串

1.字符串下标和切片

2.字符串的 in 和 not in 操作符

3.常用的字符串方法

1.字符串方法 upper()、lower()、isupper()和 islower()

2.isX 字符串方法

3.字符串方法 startswith()和 endswith()

4.字符串方法 join()和 split()

5.用 rjust()、ljust()和 center()方法对齐文本

6.用 strip()、rstrip()和 lstrip()删除空白字符


一、字典

1.字典的基本介绍

        字典是可迭代的、可变的数据结构,通过键来访问元素 。字典结构比较复杂,它是由两部分视图构成的, 是键视图,另一个是值视图 。键视图不能包含重复元素,而值集合可以,键和值是成对出现的。

2.创建字典

        字典类型是 dict ,创建字典可以使用 dict 函 数,或者用大括号{}将“键 ·值” 对括起来,“键:值”对之间用冒号分隔。示例代码如下: 

dict0={1:'张三',2:'李四',3:'王五'}
print(len(dict0))
print(dict0)
print(type(dict0))
# >>>3
# >>>{1: '张三', 2: '李四', 3: '王五'}
# >>><class 'dict'>
del dict
dict()
a=dict([(6,'张三'),(5,'李四'),(4,'王五')])
print(a)
# >>>{6: '张三', 5: '李四', 4: '王五'}
b=dict(a='a', b='b', t='t')
print(b)
# >>>{'a': 'a', 'b': 'b', 't': 't'}

3.keys()、values()和 items()方法

        有 3 个字典方法,它们将返回类似列表的值,分别对应于字典的键、值和键-值对:keys()、values()和 items()。这些方法返回的值不是真正的列表,它们不能被修改,没有append()方法。但这些数据类型(分别是 dict_keys、dict_values 和 dict_items)可以用于for 循环。示例代码如下:

dict0={1:'张三',2:'李四',3:'王五'}
for v in dict0.values():
    print(v)
for k in dict0.keys():
    print(k)
for i in dict0.items():
    print(i)

4.检查字典中是否存在键或值

        in 和 not in 操作符可以检查值是否存在于列表中。也可以利用这些操作符,检查某个键或值是否存在于字典中。示例代码如下:

dict0={1:'张三',2:'李四',3:'王五'}
print('张三' in dict0.values())
print(1 in dict0.keys())
print((1,'张三') in dict0.items())
# >>>True
# >>>True
# >>>True

5.get()方法

        在访问一个键的值之前,需要检查该键是否存在于字典中。字典有一个get()方法,它有两个参数:要取得其值的键,以及如果该键不存在时,返回的备用值。示例代码如下:

dict1={'egg':2,'apple':1}
print('我有'+str(dict1.get('egg',0))+'个鸡蛋')
print('我有'+str(dict1.get('pig',0))  +'头猪')
# >>>我有2个鸡蛋
# >>>我有0头猪

6.setdefault()方法

        传递给该方法的第一个参数,是要检查的键。第二个参数,是如果该键不存在时要设置的值。如果该键确实存在,方法就会返回键的值。示例代码如下:

dict1={'egg':2,'apple':1}
print(dict1.setdefault('pig',2))
print(dict1)
print(dict1.setdefault('egg',3))
# >>>2
# >>>{'egg': 2, 'apple': 1, 'pig': 2}
# >>>2

7.其他方法

类型

名称

功能

更新

dict.updata(dict2)

把字典dict2的键值更新到字典dict1

删除键值

dict.popitem()

返回并删除字典最后一对的键值

dict.pop(key[,default])

删除键key,返回被删除的值。Key不存在则返回default

dict.clear()

清空字典的所有元素


二、字符串

1.字符串下标和切片

字符串像列表一样,使用下标和切片。可以将字符串'Hello world!'看成是一个列表,字符串中的每个字符都是一个表项,有对应的下标。

 

2.字符串的 in 和 not in 操作符

        像列表一样,in 和 not in 操作符也可以用于字符串。用 in 或 not in 连接两个字符串得到的表达式,将求值为布尔值 True 或 False。

3.常用的字符串方法

1.字符串方法 upper()、lower()、isupper()和 islower()

        upper()和 lower()字符串方法返回一个新字符串,其中原字符串的所有字母都被相应地转换为大写或小写。字符串中非字母字符保持不变。如果字符串至少有一个字母,并且所有字母都是大写或小写,isupper()和islower()方法就会相应地返回布尔值 True。否则,该方法返回 False。示例代码如下:

a='hello world'
print(a)
print(a.upper())
print(a.isupper())
print(a.lower())
print(a.islower())
b='HELLO world'
print(b.isupper())
print(b.islower())
# >>>hello world
# >>>HELLO WORLD
# >>>False
# >>>hello world
# >>>True
# >>>False
# >>>False

2.isX 字符串方法

        除了 islower()和 isupper(),还有几个字符串方法,它们的名字以 is 开始。这些方法返回一个布尔值,描述了字符串的特点。下面是一些常用的 isX 字符串方法:

        isalpha()返回 True,如果字符串只包含字母,并且非空;

        isalnum()返回 True,如果字符串只包含字母和数字,并且非空;

        isdecimal()返回 True,如果字符串只包含数字字符,并且非空;

        isspace()返回 True,如果字符串只包含空格、制表符和换行,并且非空;

        istitle()返回 True,如果字符串仅包含以大写字母开头、后面都是小写字母的单词。

        示例代码如下:

print('hello'.isalpha())
print('hello123'.isalpha())
print('hello123'.isalnum())
print('123'.isdecimal())
print(' '.isspace())
print('This Is Title Case'.istitle())
# >>>True
# >>>False
# >>>True
# >>>True
# >>>True
# >>>True

3.字符串方法 startswith()和 endswith()

        startswith()和 endswith()方法返回 True,如果它们所调用的字符串以该方法传入的字符串开始或结束。否则,方法返回 False。示例代码如下:

print('hello world'.startswith('hello'))
print('hello world'.startswith('world'))
print('hello world'.endswith('world'))
# >>>True
# >>>False
# >>>True

4.字符串方法 join()和 split()

        join()方法在一个字符串上调用,参数是一个字符串列表,返回一个字符串。返回的字符串由传入的列表中每个字符串连接而成。split()方法做的事情正好相反:它针对一个字符串调用,返回一个字符串列表。示例代码如下:

a=['My','name','is','peter']
print(' '.join(a))
print(','.join(a))
b='My name is peter'
print(b.split(' '))
b='My,name,is,peter'
print(b.split(','))
# >>>My name is peter
# >>>My,name,is,peter
# >>>['My', 'name', 'is', 'peter']
# >>>['My', 'name', 'is', 'peter']

5.用 rjust()、ljust()和 center()方法对齐文本

        rjust()和 ljust()字符串方法返回调用它们的字符串的填充版本,通过插入空格来对齐文本。这两个方法的第一个参数是一个整数长度,用于对齐字符串。

        center()字符串方法与 ljust()与 rjust()类似,但它让文本居中,而不是左对齐或右对齐。示例代码如下:

a='hello'
print(a.rjust(10))
print(a.ljust(10))
print(a.center(10))
print(a.center(11,'-'))
#      hello
# hello     
#   hello   
# ---hello---

6.用 strip()、rstrip()和 lstrip()删除空白字符

        strip()字符串方法将返回一个新的字符串,它的开头或末尾都没有空白字符。

        lstrip()和 rstrip()方法将相应删除左边或右边的空白字符。

        示例代码如下:

string = '  hello world  '
print(string.strip())
print(string.rstrip())
print(string.lstrip())
# hello world
#   hello world
# hello world

都看到这里了,觉得有用就点个赞吧!!!

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LLGululu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值