Python使用内置函数dict()创建字典,使用count方法创建字符串,使用format()方法格式化字符,join()方法的实现举例,remove()方法的实现举例

目录

一、前言

二、使用内置函数dict()创建字典

2.1代码段

2.2实现结果(流程)

 三、使用count方法创建字符串

3.1代码段

3.2实现结果(流程)

四、使用format()方法格式化字符

 4.1代码段

4.2实现结果(流程)

五、join()方法的实现举例

5.1代码段                  

5.2实现结果(流程)

六、remove()方法的实现举例

6.1代码段

6.2实现结果(流程)


一、前言

        下面会将Python的部分使用方法及函数整合在一起,有需要的可以点击目录来查看代码及运行的截图;
        以下代码均为举例,如有需要可以自己改写。原则上代码都是正确的,但也有可能部分地方存在逻辑错误,请谅解!

       举例的语法函数有:使用内置函数dict()创建字典,使用count方法创建字符串,使用format()方法格式化字符,join()方法的实现举例,remove()方法的实现举例;

二、使用内置函数dict()创建字典

2.1代码段

stu_info1={'num':'20180101','name':'Liming','sex':'male'}
stu_info2=dict(stu_info1)
stu_info3=dict([('num','20180101'),('name','Liming'),('sex','male')])
stu_info4=dict(num='20180101',name='Liming',sex='male')
stu_info5=dict(zip(['num','name','sex'],['20180101','Liming','male']))
if stu_info1==stu_info2==stu_info3==stu_info4==stu_info5:
    print("创建字典的5种方式相同")
else:
    print("创建字典的5种方式不相同")

2.2实现结果(流程)

2.2.1方式相同:

2.2.2方式不相同:

 三、使用count方法创建字符串

3.1代码段

ml="t=This is a Python book!"
a=ml.count("is")
b=ml.count("is",1,6)
print(a)
print(b)

3.2实现结果(流程)

四、使用format()方法格式化字符

 4.1代码段

a=66
b=88888888.88888
strs="I very love Python!"
print("a={0:05}".format(a))
print("b={0:,.3f}".format(b))
print("{0:*^30}".format(strs))

4.2实现结果(流程)

五、join()方法的实现举例

5.1代码段                  

zb="This       is        a        Python"
zb=zb.split()
print(zb)
yq=' '.join(zb)
print(yq)

5.2实现结果(流程)

六、remove()方法的实现举例

6.1代码段

x = ['123','abc','xyz','abc','python']		
while 'abc' in x:							
    x.remove('abc')							
print(x)									

6.2实现结果(流程)

猜你喜欢

转载自blog.csdn.net/weixin_59042297/article/details/129188906
今日推荐