标签:常用 字符串类型 hello 玩法 spl 描述 span form apt
一,基本使用
1.int用途:年龄,号码,等级等。。。
2.定义方式
age=18 #age=int(18)
x=int(‘111‘) #int 只能将纯数字的字符串换成十进制的整型。
print(type(x))
3,常用操作和内置方法
算术运算和比较运算
二:该类型总结:
1,存一个值还是多个值?
只能存一个值。
例如:age=18
2.有序还是无序?(看是否有索引)
没有无序这么一说。
3.可变 还是不可变?
#可变指的是改变原值,即在原值基础上进行修改
#可变类型的底层原理:在id不变的情况下,值可以改变
#int 是不可变数据类型。
了解(**)
# 长整型Long,只有在python2中才有长整型号
# 复数complex
# x=1-2j
# print(x.real# print(x.imag)
# print(type(x))
字符串类型
一,基本使用
1.用途:描述性质的数据,比如人的名字,单个爱好,地址。
2.定义方式:
name=‘egon‘ #name=str(‘egon‘)
# x=str(1)
# y=str(1.1)
# z=str([1,2,3])
# n=str({‘a‘:1})
# print(type(x))
# print(type(y))
# print(type(z))
# print(type(n))
3.常用操作+内置方法
优先掌握的操作(*****)
1.按照索引取值(正向取+反向取):只能取
msg=‘hello world‘
print(type(msg[5])) #取出的就是字符串类型
print(msg[-1]) #取出的是d
2. 切片(骨头不顾尾,步长)
msg=‘hello world‘
print(msg[1:5],type(msg[1:5])) #取出的是 ello ,字符串类型
print(msg[6:-1]) #取出的是 worl
print(msg[6:11]) #取出的是world
print(msg[6:]) #取出的是world
print(msg[6::2]) #后面的2代表步长为2,取出的是wrd
# 了解(**)
# print(msg[0:])
# print(msg[::-1])
# msg=‘hello world‘
# print(msg[-3:-6:-1])
# print(msg[6:9:-1])
3.长度len
print(len(msg)) #取出的是11
4.成员运算 in 和 not in
print(‘sb‘in ‘my name is alex‘ ) #打出的是 False
print(‘egon‘ not in ‘my name is alex,alex is sb‘) #打出的是True, 推荐使用
# print(not ‘egon‘ in ‘my name is alex,alex is SB‘) #打出的是 True
5.移除空白strip
name=‘ e gon ‘
print(name.strip())
print(name.strip())
# name=‘****A*e*gon****‘
print(name.strip(‘*‘)) #打印出来的是 A*e*gon
name=‘****egon****‘
print(name.lstrip(‘*‘)) #打出的是 egon**** 去除左边的*
print(name.rstrip(‘*‘)) #打出的是****egon 移除右边的*
例如:
# pwd=input(‘>>: ‘).strip() #pwd=‘123 ‘
# if pwd == ‘123‘:
# print(‘login successful‘)
msg=‘cccabcdefgccccc‘
print(msg.strip(‘c‘)) #取出的值是abcdefg 从左向右和从右向左依次找c,碰到不是c的就不移除,
print(‘*-=egon *&^‘.strip(‘-= *&^‘)) 打印出egon
#6、切分split
# msg=‘egon:18:male:180:160‘
# l=msg.split(‘:‘)
# print(l)
# print(l[3])
#7、循环
# msg=‘hello world‘
# for x in msg:
# print(x)
# 需要掌握的操作(****)
#1、strip,lstrip,rstrip
#2、lower,upper
# name=‘EoN‘
# print(name.lower())
# name=‘egonN‘
# print(name.upper())
#3、startswith,endswith
# print(‘alex is SB‘.startswith(‘alex‘))
# print(‘alex is SB‘.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(‘egon‘,18))
# print(‘my name is {0} my age is {1} {1} {1} {1}‘.format(‘egon‘,18))
#5、split,rsplit
# info=‘egon:18:male‘
# print(info.split(‘:‘,1))
# print(info.split(‘:‘,1)) #[‘egon‘,‘18:male‘]
# print(info.rsplit(‘:‘,1)) #[‘egon:18‘,‘male‘]
#6、join:只能将元素全为字符串的列表拼成一个大的字符串
# info=‘egon:18:male‘
# l=info.split(‘:‘)
# print(l)
# new_info=‘-‘.join(l)
# print(new_info)
# num=[‘a‘,‘b‘,‘c‘]
# ‘:‘.join(num) #‘a‘+‘:‘+‘b‘+‘:‘+‘c‘
# num=[1,2,‘c‘]
# ‘:‘.join(num) #1+‘:‘+2+‘:‘+‘c‘
#7、replace
# msg=‘my name is wupeiqi,wupeiqi is SB‘
# print(msg.replace(‘wupeiqi‘,‘Pig‘,1))
# print(msg)
#8、isdigit
# print(‘111.1‘.isdigit())
# print(‘1111‘.isdigit())
# AGE=73
# age=input(‘>>: ‘).strip() #age=‘asdfasdf‘
# if age.isdigit():
# age=int(age)
# if age > AGE:
# print(‘too big‘)
# elif age < AGE:
# print(‘too small‘)
# else:
# print(‘you got it‘)
# else:
# print(‘必须输入数字啊傻叉‘)
# 其他操作(了解即可)(**)
#1、find,rfind,index,rindex,count
msg=‘my name is alex,alex is hahaha‘
# print(msg.find(‘alex‘))
# print(msg.find(‘SB‘)) #找不到会返回-1
# print(msg.index(‘alex‘))
# print(msg.index(‘SB‘)) # 找不到index会报错
# print(msg.find(‘alex‘,0,3))
# print(msg.count(‘alex‘))
# print(msg.count(‘alex‘,0,15))
#2、center,ljust,rjust,zfill
# print(‘info egon‘.center(50,‘-‘))
# print(‘info egon‘.ljust(50,‘-‘))
# print(‘info egon‘.rjust(50,‘-‘))
# print(‘info egon‘.zfill(50))
#3、expandtabs
# print(‘a\tb\tc‘.expandtabs(1))
#4、captalize,swapcase,title
# print(‘my name is egon‘.capitalize())
# print(‘my Name Is egon‘.swapcase())
# print(‘my name is egon‘.title())
#5、is数字系列
num1=b‘4‘ #bytes
num2=u‘4‘ #unicode,python3中无需加u就是unicode
num3=‘壹‘ #中文数字
num4=‘Ⅳ‘ #罗马数字
#isdigit():bytes,unicode
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())
#isdecimal():unicode
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())
#isnumberic;unicode,中文,罗马
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())
#6、is其他
# print(‘abasdf123123‘.isalnum())
# print(‘asdfasdf‘.isalpha())
# print(‘egon‘.islower())
# print(‘ABC‘.isupper())
# print(‘ ‘.isspace())
# print(‘My Name Is Egon‘.istitle())
# #二:该类型总结
# 1 存一个值or存多个值
# 只能存一个值
#
# 2 有序or无序
# 有序
# 3 可变or不可变
# 不可变
name=‘egon‘
print(id(name))
name=‘alex‘
print(id(name))
列表类型
#6、切分split
# msg=‘egon:18:male:180:160‘
# l=msg.split(‘:‘)
# print(l)
# print(l[3])
#7、循环
# msg=‘hello world‘
# for x in msg:
# print(x)
# 需要掌握的操作(****)
#1、strip,lstrip,rstrip
#2、lower,upper
# name=‘EoN‘
# print(name.lower())
# name=‘egonN‘
# print(name.upper())
#3、startswith,endswith
# print(‘alex is SB‘.startswith(‘alex‘))
# print(‘alex is SB‘.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(‘egon‘,18))
# print(‘my name is {0} my age is {1} {1} {1} {1}‘.format(‘egon‘,18))
#5、split,rsplit
# info=‘egon:18:male‘
# print(info.split(‘:‘,1))
# print(info.split(‘:‘,1)) #[‘egon‘,‘18:male‘]
# print(info.rsplit(‘:‘,1)) #[‘egon:18‘,‘male‘]
#6、join:只能将元素全为字符串的列表拼成一个大的字符串
# info=‘egon:18:male‘
# l=info.split(‘:‘)
# print(l)
# new_info=‘-‘.join(l)
# print(new_info)
# num=[‘a‘,‘b‘,‘c‘]
# ‘:‘.join(num) #‘a‘+‘:‘+‘b‘+‘:‘+‘c‘
# num=[1,2,‘c‘]
# ‘:‘.join(num) #1+‘:‘+2+‘:‘+‘c‘
#7、replace
# msg=‘my name is wupeiqi,wupeiqi is SB‘
# print(msg.replace(‘wupeiqi‘,‘Pig‘,1))
# print(msg)
#8、isdigit
# print(‘111.1‘.isdigit())
# print(‘1111‘.isdigit())
# AGE=73
# age=input(‘>>: ‘).strip() #age=‘asdfasdf‘
# if age.isdigit():
# age=int(age)
# if age > AGE:
# print(‘too big‘)
# elif age < AGE:
# print(‘too small‘)
# else:
# print(‘you got it‘)
# else:
# print(‘必须输入数字啊傻叉‘)
# 其他操作(了解即可)(**)
#1、find,rfind,index,rindex,count
msg=‘my name is alex,alex is hahaha‘
# print(msg.find(‘alex‘))
# print(msg.find(‘SB‘)) #找不到会返回-1
# print(msg.index(‘alex‘))
# print(msg.index(‘SB‘)) # 找不到index会报错
# print(msg.find(‘alex‘,0,3))
# print(msg.count(‘alex‘))
# print(msg.count(‘alex‘,0,15))
#2、center,ljust,rjust,zfill
# print(‘info egon‘.center(50,‘-‘))
# print(‘info egon‘.ljust(50,‘-‘))
# print(‘info egon‘.rjust(50,‘-‘))
# print(‘info egon‘.zfill(50))
#3、expandtabs
# print(‘a\tb\tc‘.expandtabs(1))
#4、captalize,swapcase,title
# print(‘my name is egon‘.capitalize())
# print(‘my Name Is egon‘.swapcase())
# print(‘my name is egon‘.title())
#5、is数字系列
num1=b‘4‘ #bytes
num2=u‘4‘ #unicode,python3中无需加u就是unicode
num3=‘壹‘ #中文数字
num4=‘Ⅳ‘ #罗马数字
#isdigit():bytes,unicode
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())
#isdecimal():unicode
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())
#isnumberic;unicode,中文,罗马
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())
#6、is其他
# print(‘abasdf123123‘.isalnum())
# print(‘asdfasdf‘.isalpha())
# print(‘egon‘.islower())
# print(‘ABC‘.isupper())
# print(‘ ‘.isspace())
# print(‘My Name Is Egon‘.istitle())
# #二:该类型总结
# 1 存一个值or存多个值
# 只能存一个值
#
# 2 有序or无序
# 有序
# 3 可变or不可变
# 不可变
name=‘egon‘
print(id(name))
name=‘alex‘
print(id(name))
标签:常用 字符串类型 hello 玩法 spl 描述 span form apt
原文地址:https://www.cnblogs.com/fxc-520520/p/9112484.html