Python学习之路_day_04(字符串与列表的内置方法)

摘要:
您只能接受msg='helloworld'打印(msg[4])打印(msg[-1])msg[3]='name='gon'2。切片(不考虑终点,步长)msg='helloworld'#是剪切一个新的子字符串打印(msg[0:1])msg='elloworld'print(msg[5:

一、字符串内置方法的基本使用
1 用途:记录描述性的状态,比如人的名字、地址、性别

2 定义方式:在"",'',""""""内包含一系列的字符
msg='hello' #msg=str('hello')
res1=str(1)
res2=str([1,2,3])
print(type(res1),type(res2))
info="'xxx'"

3 常用操作+内置的方法
优先掌握的操作:
1、按索引取值(正向取+反向取) :只能取
msg='hello world'
print(msg[4])
print(msg[-1])
msg[3]='A'
name='egon'

2、切片(顾头不顾尾,步长)
msg='hello world' # 就是从一个大的字符串中切分出一个全新的子字符串
print(msg[0:5])
print(msg) # 没有改变原值

print(msg[0:5:1])
print(msg[0:5])
print(msg[0:5:2]) #hlo

了解:
print(msg[0:5:1])
msg='hello world'
print(msg[5:0:-1])
print(msg[5::-1])
print(msg[-1::-1])
print(msg[::-1])

3、长度len
msg='hello world'
print(len(msg))

4、成员运算in和not in: 判断一个子字符串是否存在于一个大的字符串中
print('alex' in 'alex is dsb')
print('dsb' in 'alex is dsb')
print('sb' in 'alex is dsb')
print('xxx' not in 'alex is dsb') # 推荐
print(not 'xxx' in 'alex is dsb')

5、去掉字符串左右两边的字符strip,不管中间的

user='      egon       '
user='   x   egon       '
user="*******egon********"
user=" **+*  */***egon*  **-*****"
print(user.strip("* +/-"))

user=input('>>>: ').strip()
if user == "egon":
    print('用户名正确')

6、切分split:针对按照某种分隔符组织的字符串,可以用split将其切分成列表,进而进行取值

msg="root:123456:0:0::/root:/bin/bash"
res=msg.split(':')
print(res[1])

cmd='dowload|a.txt|3333333'
cmd_name,filename,filesize=cmd.split('|')

7、循环
msg='hello'
for item in msg:
    print(item)

二、字符串的内置方法

需要你掌握的
1、strip,lstrip,rstrip
print('*****egon*****'.lstrip('*'))
print('*****egon*****'.rstrip('*'))
print('*****egon*****'.strip('*'))

2、lower,upper
msg='aABBBBb'
res=msg.lower()
print(res)
print(msg)

3、startswith,endswith
msg='alex is dsb'
print(msg.startswith('alex'))
print(msg.endswith('sb'))
print(msg.endswith('b'))

4、format的三种玩法
print('my name is %s my age is %s' %('egon',18))
print('my name is {name} my age is {age}'.format(age=18,name='egon'))

了解
print('my name is {} my age is {}'.format(18,'egon'))
print('my name is {0} my age is {1}{1}'.format(18,'egon'))

5、split,rsplit
msg='get|a.txt|333331231'
# print(msg.split('|',1))
print(msg.split('|',1))
print(msg.rsplit('|',1))

6、join
msg='get|a.txt|333331231'
l=msg.split('|')
print(l)

src_msg='|'.join(l)
print(src_msg)

7、replace
msg='alex say i have one tesla,alex is alex hahaha'
print(msg.replace('alex','sb',1))
print(msg)

8、isdigit # 判断字符串中包含的是否为纯数字
print('10.1'.isdigit())
age=input('>>: ').strip()
if age.isdigit():
    age=int(age) #int('asfdsadfsd')
    if age > 30:
        print('too big')
    elif age < 30:
        print('too small')
    else:
        print('you got it')
else:
    print('必须输入数字')

需要了解的内置方法
1、find,rfind,index,rindex,count
msg='hello alex is sb'
print(msg.find('alex'))
print(msg.find('alex',0,3))

print(msg.index('alex'))
print(msg.index('alex',0,3))

msg='alex aaa alex'
print(msg.find('alex'))
print(msg.rfind('alex'))

msg='alex aaa alex'
print(msg.count('alex')) # 统计一个子字符串在大字符串中出现的次数

2、center,ljust,rjust,zfill
print('egon'.center(50,'*'))
print('egon'.ljust(50,'*'))
print('egon'.rjust(50,'*'))
print('egon'.zfill(50))

3、expandtabs
print('a b'.expandtabs(1))

4、captalize,swapcase,title
print('hello'.capitalize())
print('hElLo'.swapcase())
print('egon is nb'.title())

5、is数字系列
在python3中
num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='壹' #中文数字
num4='Ⅳ' #罗马数字

''.isnumeric(): unicode,中文数字,罗马数字
print(num2.isnumeric())
print(num3.isnumeric())
print(num4.isnumeric())

''.isdecimal(): unicode
print(num2.isdecimal())
print(num3.isdecimal())
print(num4.isdecimal())

''.isdigit() :bytes,unicode
print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit())

6、is其他
print('abc你'.isalpha()) # 字符串中包含的是字母或者中文字符

字符串中包含的是字母(中文字符)或数字
print('ab'.isalnum())
print('123123'.isalnum())
print('ab123'.isalnum())

三、列表该类型总结
1 存一个值

2 有序

3 不可变
x='abc'
print(id(x))
x='def'
print(id(x))

一:基本使用
1 用途:存放多个值,可以根据索引存取值

2 定义方式:在[]内用逗号分割开多个任意类型的值
l=['egon','lxx','yxx'] # l=list(['egon','lxx','yxx'])
l1=list('hello') #list就相当于调用了一个for循环依次取出'hello'的值放入列表
print(l1)
l2=list({'x':1,'y':2,'z':3})
print(l2)
list(10000) # 报错

3 常用操作+内置的方法
优先掌握的操作:
1、按索引存取值(正向存取+反向存取):即可存也可以取
l=['egon','lxx','yxx']
print(l[0])
l[0]='EGON'
print(l)
print(l[-1])
print(l[3])
l[0]='EGON' # 只能根据已经存在的索引去改值
l[3]='xxxxxxxx' #如果索引不存在直接报错

2、切片(顾头不顾尾,步长)
l=['egon','lxx','yxx',444,555,66666]
print(l[0:5])
print(l[0:5:2])
print(l[::-1])

3、长度
l=['egon','lxx','yxx',444,555,66666,[1,2,3]]
print(len(l))

4、成员运算in和not in
l=['egon','lxx','yxx',444,555,66666,[1,2,3]]
print('lxx' in l)
print(444 in l)

5、追加
l=['egon','lxx','yxx']
l.append(44444)
l.append(55555)
print(l)

6、往指定索引前插入值
l=['egon','lxx','yxx']
l.insert(0,11111)
print(l)
l.insert(2,2222222)
print(l)

7、删除
l=['egon','lxx','yxx']

单纯的删除值:
方式1:
del l[1] # 通用的
print(l)

方式2:
res=l.remove('lxx') # 指定要删除的值,返回是None
print(l,res)

从列表中拿走一个值
res=l.pop(-1) # 按照索引删除值(默认是从末尾删除),返回删除的那个值
print(l,res)

8、循环
l=['egon','lxx','yxx']
for item in l:
    print(item)

需要掌握的操作
l=['egon','egon','lxx','yxx',444,555,66666]
print(l.count('egon'))
print(l.index('egon'))
print(l.index('yxx',0,1))
l.clear()

items=['a','b','c']
items='hello'
for item in items:
    l.append(item)
l.extend(items)
print(l)

l=['egon','egon','lxx','yxx',444,555,66666]
l.reverse()
print(l)

nums=[3,-1,9,8,11]
nums.sort(reverse=True)
print(nums)

items=[1,'a','b',2]
items.sort()

列表二、该类型总结
1 存多个值

2 有序

3 可变
l=['a','b','c']
print(id(l))
l.append('d')
print(id(l))

队列:先进先出
l=[]
# 入队
l.append('first')
l.append('second')
l.append('third')
print(l)
# 出队
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))

堆栈:先进后出
l=[]
# 入栈
l.append('first')
l.append('second')
l.append('third')
print(l)
出栈
print(l.pop())
print(l.pop())
print(l.pop())

免责声明:文章转载自《Python学习之路_day_04(字符串与列表的内置方法)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇文件处理,文件的三种打开模式,绝对路劲和相对路径,with管理网文件上下文,文件的高级应用,文件的修改,登陆注册。Linux命令行抓包及包解析工具tshark(wireshark)使用实例解析下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

丝杠推力计算器

背景来由 为什么会做这个丝杠推力计算器呢。 首先因为目前的工作需要经常做丝杠和电机匹配的选型,手工计算丝杠推力个人觉得还是比较麻烦的,一个公式需要翻来覆去的变换才能计算不同的变量值。 再有 WIN 10 提供的计算器科学模式居然没有 pi 值,计算起来非常不方便,而且结果不能保存相互对比,所以就有了这个丝杠推力计算器。 简单介绍 丝杠推力计算器可以任意计算...

Halcon 学习笔记--颜色识别(7)

一、颜色         RGB色彩模式是工业界的一种颜色标准,是通过对红(R)、绿(G)、蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,RGB即是代表红、绿、蓝三个通道的颜色,这个标准几乎包括了人类视力所能感知的所有颜色,是运用最广的颜色系统之一。        HSV 是根据颜色的直观特性创建的一种颜色空间, 也称六角锥体模型...

【计算机视觉领域】常用的 feature 提取方法,feature 提取工具包

  【计算机视觉领域】常用的 feature 提取方法,feature 提取工具包     利用 VL 工具包进行各种特征的提取:   VL 工具包官网地址:http://www.vlfeat.org/index.html   %% Extract Every kind of Features% the VL_tool box for fea...

Avue-curd通用模板(二)

目录 Avue-curd通用模板 1、增加路由菜单 2、通用模板 3、踩坑 4、表格空数据样式 Avue-curd通用模板 上一篇已经把所有使用Avue的准备工作做好了,下面就通过一个简单的例子来体验一下avue能带给我们的便利。 1、增加路由菜单 在 src/router/index.js,增加一个路由(由于这只是用来测试学习的,所以放在了...

python实现websocket

# websocket实现原理 ''' 1.服务端开启socket,监听ip和端口 2.客户端发送连接请求(带上ip和端口) 3.服务端允许连接 4.客户端生成一个随机字符串,和magic string组合进行一个sha1加密,加密。并将随机字符串发送给服务端 5.然后服务端也要用相同的方式进行加密。 6.然后服务端将加密之后的密串返回给客户...

Python笔记---错误笔记

Python---错误笔记 1. Python编码问题: 我们在编写 Python 脚本时,往往会写上中文凝视。可是有时候,当我们执行程序时。却发现例如以下错误:SyntaxError: Non-ASCII character 'xe5' in file /home/johnnie/Files/Workspace/python/head_first/ch...