1. 程式人生 > >字串型別和列表型別的使用

字串型別和列表型別的使用

字串型別的使用:

# #一:基本使用
# # 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'
# msg='hello world'
# print(msg[-1])
# msg[1]='H'
# print(msg)

# #2、切片(顧頭不顧尾,步長)
# msg='hello world' # 就是從一個大的字串中切分出一個全新的子字串
# # print(msg[0:5])
# # print(msg) # 沒有改變原值
# print(msg[0:5:2])
# # 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])
# #
# 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('ddd'in 'dddddssss')
# 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("* +/-"))
# 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)

# cmd='dowload|a.txt|3333333'
# cmd_name,filename,filesize=cmd.split('|')
# print(cmd_name,filename,filesize)
# #7、迴圈
# msg='hello'
# for item in msg:
# print(item)
#
# # 需要你掌握的
# 1、strip,lstrip,rstrip
# print('*****egon*****'.strip('*'))
# 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'))
# print(msg.startswith('a'))
# print(msg.endswith(' dsb'))
# #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 {name} my age is {age}'.format(age=22,name='wang'))
# # 瞭解
# # print('my name is {} my age is {}'.format(18,'egon'))
# print('my name is {0}{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 tesla hahaha'
# # print(msg.replace('alex','sb',1))
# print(msg.replace('tesla','bike',))
# 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('is'))
# print(msg.find('alex',0,3))

# print(msg.index('alexxxxx'))
# 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\tb'.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)
# l.insert(1,5555555)
# print(l)
# l.insert(2,2222222)
# print(l)

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

# 單純的刪除值:
# 方式1:
# del l[1] # 通用的
# del l[0]
# print(l)

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

# 從列表中拿走一個值
# res=l.pop(-1) # 按照索引刪除值(預設是從末尾刪除),返回刪除的那個值
# res=l.pop(-2)
# 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.count('egon'))
# print(l.index('egon',0,4))
# print(l.index('yxx',0,1))
# l.clear()
# print(l)
# items=['a','b','c']
# # items='hello'
# for item in items:
# l.append(item)
# # l.extend(items)
# print(l)
# extend()函式沒有返回值,直接在已存在的列表中新增新列表內容

# 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))
# lst = ["王志⽂文", "張⼀一⼭山", "苦海海⽆無涯"]
# lst.extend(["麻花藤", "麻花不不疼"])
# print(lst)

# 佇列:先進先出
# 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())