苹果电脑快捷键:control+空格:中文;Ctrl为command
内存介于CPU和硬盘之间,CPU运行快,容量小;硬盘容量大,运行速度慢。
一个字符在内存中占有一个字节str1="hello"
str2="world"
str3=str1+str2#
#hello world 格式化符,这是两种组成字符串的处理方式
#字符串的切片
print('str3')
切片 find() rfind() index() rindex() count() replace()的使用
#encoding=utf-8
str1="hello"
str2="world"
str3=str1+str2#helloworld
strf= "hello %s"%(str2)
name="abcedefefageg" #切片的使用
name13=name[1:4]#bce
startpos=2
endpos=7
namese=name[startpos:endpos]# cedef从开始的位置startpos取,到endpos,但是不包括endpos
fullname=name[:] #复制
minuname=name[ :-1]#取到倒数第一个g,abcedefefageg
oddname=name[::2]#acdffgg 取字符的间隔为2 [起始位置:结束位置:步长]
revername=name[-1:0:-1]#第一个没有取到'rgegafefedecb'
revename=name[-1::-1]#逆序:rgegafefedecba
rename=name[::-1]#步长为负,从右到左;步长为正,从左向右rgegafefedecba
#find 检测字符串
name1="learn pyton is a happy thing"
pos=name1.find('n') #返回字符串第一次开始出现的位置 4 下标;不存在返回-1
rpos=name1.rfind('n')#从字符串右边开始查询 ,第一次出现的下标 26
posIndex=name1.index('is')#索引12
#find index的相同点:都是查找字符串,返回字符串的下标;区别:find找不到返回-1,index找不到error,出现异常
rposIndex=name1.index('is')#12 逆序查找
#count
count=name1.count('a',0,5)#在1-5之间出现了多少次a(有范围) 1,若无,返回0
countFull=name1.count('y') #2 所有出现的次数
replacename="I like to learn python"
replacepython=replacename.replace('python','C#')#I like to learn C#原字符串没有发生改变,字符串的不变性,返回的字符串发生了改变
replaceL=replacename.replace('l','L')#所有的字符都会发生替换 I Like to Learn python
replaceCount=replacename.replace('l','L',1)#后面是会替换字符的个数I Like to learn python
split() capitalize() title() startswith() endswith() lower() upper() ljust() rjust() center() lstrip() rstrip() parition() rpartition()的使用
#split 分割,成了列表
splitName=name1.split(' ')#按照后面的空格进行切割'learn', 'pyton', 'is', 'a', 'happy', 'thing'
splitsName=name1.split()#括号中没有参数时,以出现的除字符以为的多个字符为分隔,如‘ ’ ‘\n’
#capitalize 将字符串的第一个字符大写
capitalizeName=name1.capitalize()#Learn pyton is a happy thing
#title 将字符串的每个单词首字母大写
titleName=name1.title()#Learn Pyton Is A Happy Thing'
#startswith 检测字符串是否以 后面的字符开头,是返回true,否则返回false
startWithName=name1.startswith('I')#False
#endswith 检测字符串是否以 后面的字符结尾,是返回true,否则返回FALSE
endsWithName=name1.endswith('thing')#True
filename='****.txt'
fileTxt=filename.endswith('.txt')#True ,检查后缀
#lower将所有的大写字母转为小写字母
#upper将所有的小写字母转为大写字母
lowerName=name1.lower()#'learn pyton is a happy thing'
upperName=name1.upper()#'LEARN PYTON IS A HAPPY THING'
end="Yes"
if(end.upper()=='YES'):
print('OK')
#rjust ,返回一个字符串右对齐,并使用空格填充至长度width的新字符串
#center 居中显示
lJustName=name1.ljust(50)#靠左显示
rJustName=name1.rjust(50) #这里开始空格 learn pyton is a happy thing
centerName=name1.center(50) #前后都有空格
#去掉空格
string=' python is a good language '
lStripString=string.lstrip()#去掉左边的空格
rStripString=string.rstrip()#去掉右边的空格
stripString=string.strip()#去掉左右两边的空格
#partition 将字符以后面的字符为界限,分成三部分,
partitionString=string.partition('o')#((' pyth', 'o', 'n is a good language ')
rPartitonString=string.rpartition('o')#(' python is a ', 'good', ' language ')
splitlines() isalpha() isdigit() isalnum() isspace() join()的使用
#splitlines 按照行分隔,返回一个包含各行作为元素的列表
stringlines="Python is a good tool \n C# is also a good tool \n the same as java"
splitLineString=stringlines.splitlines()#['Python is a good tool ', ' C# is also a good tool ', ' the same as java']
#isalpha 判断所有字符都是字母,则返回True,否则返回False
isalphaString=stringlines.isalpha() #False \n
#isdigit 判断是否只包含数字,是返回True,否则返回False
#isalnum 判断所有字符是否都是字母或数字,是返回true,否则False
isDigitString=stringlines.isdigit()#False
isAlNumString=stringlines.isalnum()#False
#isspace() 字符串中只包含空格,返回True
spaceString=" "
isSpaceString=spaceString.isspace()#True
#join 字符串中每个字符后面插入给定的字符,构造出一个新的字符串
#list 和 tuple has no attrbute 'join'
List=['a','v','w']
joinString=('*').join(List)#'a*v*w' 注意join的用法
print(isSpaceString,joinString)
#这里处理一个问题的小技巧
#将List的\n ' '为分隔符,再将他们连接成字符
#分析:前面学的split()前面的分隔符只指定了一种,这里出现的两种;将分隔后生成的list连接起来,用空格
#连接的话,中间会产生空格,所有默认连接
sjString=" Today is Monday ,\nand I will go to the park \n"
sString=sjString.split()#同时实现了以' '和\n分隔
jString=('').join(sString)
print(sjString)
print(sString)
print(js)