温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python 字符类型

发布时间:2020-07-25 00:40:26 来源:网络 阅读:2647 作者:期待美好 栏目:编程语言
数字类型:
复数:
x=1+2j #1为实数,2j为虚数
print(x.real)
print(x.imag)

可变:值变,id不变,可变==不可hash
不可变:值变,id就变,不可变==可hash
print(hash(123))

数字类型和字符串类型为不可变

字符串类型:
在'',"",''' '''内定义的一串字符。
mag='hello '

1.按索引取值(正向取+反向取):只能取
print=(mag[0])
print=(mag[-1])

2.切片(顾头不顾尾,步长)也适用于列表
print(mag[0:4]) #0为首,4为尾
  hell

print(mag[1:4:2])#1为首,4为尾,2为步长,隔2取一次。
  l

print(mag[:]) #开始取到结尾

rint(mag[-1::-1]) #倒着取出所有值

3.长度:len
print(len(mag)) 等于 print(mag.__len__()) 

4.成员运算in和not in
print('llo' in mag)
print('llo' not in mag)

5.移除空白strip:只清除2边的空字符
password='   123456    '
pritn(password.strip())
password=input('>>:').strip()

lstrip(去左)rstrip(去右)

6.切分split
user='root:x:0:0::/root:/bin/bash'
res=user.split(':') #默认以空格分隔
print(res[0])
  root

path='C:\\a\\d.txt'
print(path.split('\\',1)) #1为只分隔一次

print(path.rsplit('\\',1)) #从右向左切

7.循环
mag='hel'

n=0
size=len(mag)
while n < size:
     print(mag[n])
     n+=1

for i in mag: 
    print(i)

for i in range(0,5,2):  
    print(i)

8.大小写变化
print('Ajisdf'.lower()) #变小写
print('asdF'.upper())  #变大写

9.startswith(以什么开始),endswith(以什么结尾)
ma=amk thui
print(ma.startswith('a'))
print(ma.endswith('i'))

10.format传值
print('my name is %s  age is %s' %('lx',78))
print('my name is ()  age is ()' format('lx',78))

print('my name is (x)  age is (y)' format(y=78,x='lx'))

print('my name is (0)  age is (1) you love (0)' format('lx',78))

11.join(和split相反)只用于字符串类型的列表拼接
inct='root:x:0:0:/root:/bin/bash'
l=inct.split(':')

print(':'.join(l))

12.replsce
mag='my is koko my is  toto my is bubu'
mag=mag.replsce('my','kawa',1) #默认替换所有,1为一次
print(mag)

13.isdigit判断值为是否为数字类型
password='123'
if password.isdigit():
    password=int(password)

14.find,rfind(从右找),index,rindex(查找),count(统计)
mag='my is ksis'
print(mag.find('is'))
print(mag.index('is'))
print(mag.count('is'))

15.center(填充),ljust(左对齐),rjust(右对齐),zfill(用0填充)
print('hi',center(30,'#'))  #30个字符,不够用#填充。
print('hi',ljust(30,'#'))
print('hi',rjust(30,'#'))
print('hi',zfill(30,'#'))

15.expandtabs  #用多少个空格分离2个单词
print('hello\tword'.expandtabs(10))

16.captalize(首字母大写),swapcase(大小写反转),title(每一个单词的首字母大写)
print('i is kooll'.catpalize())

17.is数字判断
num1=b'2'  #bytes
num2=u'4'  #unicode,python3中无需加u
num3='二'  #中文数字
num4='IV'  #罗马数字

isdigit,(适用bytes,unicode)
print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit())

isdecimal,(适用unicode)
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit())

isnumeric,(适用unicode,中文,罗马)
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit())

name='mis123'
print(name.isalnum())  #判断字符串是否由字母和数字组成
print(name.isalpho())  #判断字符串是否只由字母组成
print('print'.isidentifier())  #判断关键字
print('abd'.islower())  #判断全是否为小写
print('ASD'.isupper())  #判断大写
print('   '.isspace())  #判断空格
print('Adj'.istitle())  #判断标题,即判断单词的首字母大写

17.列表:
1.按索引取值(正向取+反向取):只能取
2.切片(顾头不顾尾,步长)
3.长度:len
4.循环
5.成员运算in和not in
...
mibs=['cc','aa','rr']
print(list['kasidhui']) #把字符,装换为列表

在列表后添加:
mibs.append(6)

删除:
del mibs[2]
mibs.remove('cc')

删除并取出结果:
resd=mibs.pop(2) #默认从末尾删
print(reds)

循环列表:
for i in mibs:
   print(i)

在列表内插入:
mibs=['cc','aa','rr']
mibs.insert(2,'bb')  #2为位置数,bb为值
print(mibs)
 'cc','aa','bb','rr'

清空列表的值:
print(mibs.claer())

拷贝一个列表:
mm=mibs.copy()

统计列表值的个数:
print(mm.count('aa'))

往列表里加多个值:
hh=['rt','ty']
print(mm.extend(hh))

返回列表的值的位置:
mibs=['cc','aa','rr']
print(mibs.index('aa'))

将列表的值反转:
mibs=['cc','aa','rr']
print(mibs.reverse())
['rr','aa','cc']

将列表的值排序:
hy=[11,2,8,15]
hy.sort()
print(hy)
 [2,8,11,15]

print(hy.sort(reverse=True))
 [15,11,8,2]

x='ahoe'
y='v'

x<y,判断首字母的排序,a-z从小到大,数字大于字母

18.元组:存多个值,对列表来说,元组不可变(是可以当字典的key的),主要是用于读
定义:与列表类型对比,只是[]变为()
1.按索引取值(正向取+反向取):只能取
2.切片(顾头不顾尾,步长)
3.长度:len
4.循环
5.成员运算in和not in
...
age=(11,22,33,44)  等于  age=tuple(11,22,33,44)

19.字典:存放多个值,key:value 存取值速度快,无序
定义:key必须是不可变类型(int,float,str,tuple),value可以是任意值
1.按索引取值(正向取+反向取):只能取
2.切片(顾头不顾尾,步长)
3.长度:len
4.循环
...
info={'name':'dachui','age':18,'sex':'xxx'} 等于  info=dict(age=18,sex='xxx',name='dachui')
info=dict([('name','dachui'),('age',18),('sex','xxx')])
info=dict([['name','dachui'],['age',18],['sex','xxx']])

创建一个空字典:
info={}.fromkeys(['name','age','sex'],None) #value为None

删除:
info={'name':'dachui','age':18,'sex':'xxx'}
print(info.pop('name')) #没有默认值
print(info.popitem('name':'dachui'))   

key ,value ,键值对items()
info={'name':'dachui','age':18,'sex':'xxx'}
print(info.keys())  #结果不是一个列表
print(list(info.keys()))  #结果为一个列表

print(info.values())  #结果不是一个列表
print(list(info.values()))  #结果为一个列表

print(info.items())   #结果不是一个列表
print(list(info.items())) #结果为一个列表

案列:
购物车
mas={'衣服':'200$','裤子':'2000$','车':'55555$','锤':'45454$'}
goods=[]
while True:
  for k in mas:
      print(k,mas[k])
  chice=input('商品名:').strip()
  print(chice)
  if len(chice) == 0 or chice not in mas:
      print('商品名不存在')     
      continue
  while True:
      num=input('购买个数:').strip()
      if num.isdigit():
         break
  goods.append((chice,mag[chice],int(num)))
  print('购物车',goods) 
  break

20.集合:
作用:关系运算,去重
定义:{}内用逗号分隔每个元素都必须是不可变类型,无序
1.按索引取值(正向取+反向取):只能取
2.切片(顾头不顾尾,步长)
3.长度:len
4.循环
5.成员运算in和not in
...

l={'sdf','dd','gg'}
k={'dd','gr','eer'}
交集:&  都存在的值
print(l & k)

合集:| 所有的值,去重的结果
print(l | k)

交叉补集:^ 除了交集以外的值
print(l ^ k)

差集:- 两个集合相减
print(l - k)

父集,子集: >,>= ,<,<=  父集包含子集
s1={1,2,3,4} #父集
s2={1,2,3}   #子集
print(s1 > s2) #Ture

列表转换为集合:
h=['a','b','c','a','a']
print(seat(h))

difference_update()相减并更新:
s1={1,2,3,4} 
s2={1,2,3} 
print(difference_update(s2))

discard指定删除集合的值
remove删除不在的元素会报错

isdisjoint两个集合没有共同部分时,为真

add,update添加值
.......

分类:
占用空间:集合<元组<列表<字典

存值个数:只能存一个值(数字,字符串),能存多个值(列表,元组,字典)

可变:列表,字典,集合

不可变:数字,字符串,元组

直接访问:数字

有序(序类类型):字符串,列表,元组

key值访问(映射类型):字典

无序:集合,字典

                          字符编码
1.字符按照什么标准存的,就要按照什么标准解码,此处的标准指的就是字符编码

2.unicode--->encode--->gbk  存
  gbk---->decode---->uicode 读

3.python3解释器默认使用的字符编码是UTF-8
  python2解释器默认使用的字符编码是ascii

指定解释器:#coding:gbk

4.pythone2的str就是python3的bytes
  python2的Unicode就是python3的str
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI