字符串(str)
1.str可以将任意类型转换成字符串
2.存一个值,有序,不可变
3.优先掌握的操作:
a、按索引取值(正向取+反向取) :只能取
str1=' hello word'
print(str1[0])
print(str1[1])#h
print(str[-1])
b、切片(顾头不顾尾,步长)
str1='hello word'
print(str1[0])#h
print(str1[0:])#hello word
print(str1[0:5])#hello
print(str1[0:5:2])#hl0
print(str1[0:5:1])#hello
print(str1[-1])#d
print(str1[-1:-5:-1]) #drow
print(str1[-1::-1])#drow olleh
c、长度len
d、成员运算in和not in
e、移除空白strip,lstrip,rstrip
pwd=" 123 "
print(pwd.strip(""))
pwd='****123****'
print(pwd.strip("*"))#123
print(pwd.strip("*"))#123
pwd="**\%&^123*%*"
print(pwd.strip("*%"))#\%&^123
f、切分split
split(self, sep, maxsplit)
sep分隔符
maxsplit=切分次数
str1='lz male 22 170 '
print(str1.split("lz")) #['', ' male 22 170 ']
print(str1.split(":")) #['lz male 22 170 ']
str1='lz:male:22:170 '
print(str1.split(":")) #['lz', 'male', '22', '170 ']
print(str1.split(":",1)) #['lz', 'male:22:170 ']
list= ''
print(list.split("P")) #['']
只有字符串有切分,一个空的字符串能被切分成列表
g、循环
str1='hello word'
for item in str1:
print(item)
#2、lower,upper
#3、startswith,endswith
#4、format的三种玩法
#5、split,rsplit
#6、join
#7、replace
#8、isdigit
4.需要掌握的操作:
a、strip,lstrip,rstrip
b、lower,upper
str1='hello word'
print(str1.upper()) #HELLO WORD
s=str1.upper()
print(s.lower()) #hello word
c、startswith,endswith (self, prefix, start=None, end=None)
print('lz is boy '.startswith('l')) #True
print('lz is boy'.endswith('boy')) #True
d、format (self, *args, **kwargs)
print('my name is %s my age is %s' %('egon',18)) ##my name is egon my age is 18
print('my name is %s my age is %s' %(18,"egon")) #my name is 18 my age is egone
print('my name is {x} my age is {y}'.format(x='egon',y=18)) #my name is egon my age is 18
print('my name is {x} my age is {y}'.format(y=18,x='egon')) #my name is egon my age is 18
print('my name is {0} my age is {1}'.format(18,'egon')) #my name is 18 my age is egon
f、split,rsplit (self, sep=None, maxsplit=-1)
spilt默认从左往右切分
str1="hello word"
print(str1.split(' ')) #['hello', 'word']
str1='hello:word:18:"l"'
print(str1.split(':',1)) #['hello', 'word:18:"l"']
print(str1.rsplit(':',1))#['hello:word:18', '"l"']
g、join(self, iterable)
str1='hello:word:18'
list1=(str1.split(':'))
print(list1) #['hello', 'word', '18']
str2=print(':'.join(list1)) #'hello:word:18'
h、replace(self, old, new, count=None)
str1='hello word hello'
print(str1.replace('hello',"key")) #"key word key"
print(str1.replace('hello',"key",1)) #"key word hello"
g、isdigit
print('123'.isdigit()) #True 只能判断纯数字的字符串
print('12.3'.isdigit()) #False
5. 其他操作(了解即可)
a、find,rfind,index,rindex,count
msg='hello worldaa'
print(msg.index('wo') # 6→wo在大字符串的索引
print(msg.index('wo',0,3)) #index寻找小字符串在大字符串的索引,当不存在的时候会报错
print(msg.find('wo',0,3)) #find也是寻找小字符串在大字符串的索引,当不存在的时候不会报错,找不到时候返回值-1
print(msg.count('l')) #3→l出现的次数
#2、center,ljust,rjust,zfill
name=input('>>: ').strip()
print('egon'.center(50,'='))
print(('%s' %name).center(50,'-'))
print('egon'.ljust(50,'='))#egon============================================== 左对齐,不够用=补齐
print('egon'.rjust(50,'='))#==============================================egon右对齐,不够用=补齐
print('egon'.zfill(50)) #0000000000000000000000000000000000000000000000egon
默认右对齐,不够用0补齐
#3、expandtabs
#4、captalize,swapcase,title
print('hello world'.capitalize()) #首字母大写 Hello world
print('Hello world'.swapcase()) #大写变小写,小写变大写 hELLO WORLD
print('Hello world'.title()) #单词的首字母大写 Hello World
#5、is数字系列
num1=b’4’ #bytes
num2=u’4’ #unicode,python3中无需加u就是unicode
num3=‘四’ #中文数字
num4=‘Ⅳ’ #罗马数字
isdigit: bytes,str
print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit())
isdecimal:str
print(num2.isdecimal())
print(num3.isdecimal())
print(num4.isdecimal())
isnumberic:str,中文\罗马
print(num2.isnumeric())
print(num3.isnumeric())
print(num4.isnumeric())
#6、is其他
print('aaasdfaA'.isalpha()) # 纯字母组成的字符串
print('aaasdfaA123'.isalnum()) # 字母或数字组成
print('aaasdfaA'.isalnum()) # 字母或数字组成
print(' '.isspace()) #判断是否全都是空格
print(' 12'.isspace()) #False