字符串是python中最常见的数据类型,可以用双引号或者单引号来创建

a = "python"
b = 'python'

字符串常用方法

 

find() 方法     和rfind方法

str.find('要查找的内容',开始的地方,结束的地方)

查找字符串的下标

如果没有找到就返回   -1 

rfind 和 find 作用是一样的   只不过是从右边开始查找

a = 'asdfghjkl'
print(a.find('j'),0,10)   # 6
print(a.rfind('j')) # 2

 

index()方法 和 rindex()方法

和find方法作用一样,都是要查找字符串

str.index('要查找的内容')

返回他所在的下标

找不到就会报错

可以用try异常来捕捉

rindex 和 index一样只不过是从右边开始

a = 'asdfghjklqwertyuiop'
try:
    print(a.index('y'))
except Except as e:
    print(-1)
触发异常返回-1

 

 count()   方法

查询字符串出现的次数

str.count('要查找的内容')

没有就返回0

a = 'aaabbbbccccdddddiiiiiiiiii'
ptint(a.count('a'))  # 3

 

len()方法

len(str)

计算字符串长度

mystr = "str"
print(len(mystr))       # 3

 

replace() 方法

str.replace('旧的',‘新的’,替换的次数)

把字符串替换成其他字符串

a = 'hello word python'
print('hello','HELLO',1)   # HELLO word python

 

split()  方法

字符串切片

str.split('切割的字符')

a = 'hello word python'
print(a.split(' '))    # 以空格切分   'hello' 'word' 'python'

 

capitalize() 方法

str.capitalize()

整个首字母大写

a = 'hello word python'
print(a.capitalize())  # Hello word python 

 

title()方法

str.title()

每个单词首字母大写

a = 'hello word python'
print(a.title())  # Hello Word Python

 

istitle()  方法

str.istitle() 

判断每个单词是否首字母大写

a = 'Hello Word Python'
print(‘a.istitle()’)     # true

 

startswith()   方法  和   endswith()  方法

str.startswith(需要判断的字符)

str.endswith(需要判断的字符)

判断字符串是否以某个字符作为开头

endswith()   和   startswith()   作用一样  endswith是判断是否结尾为某个字符

a = 'python'
print(a.startswith('p'))   # true

 

 lower()方法     和     upper() 方法

str.upper()

str.lower()

upper()   把所以字母大写

lower()    把所有字母都变成小写

a = 'HELLO WORD PYTHON'
print(a.lower())     # hello word python
a = 'hello word python'
print(a.upper()) # HELLO WORD PYTHON

 

 ljust()  方法   、    rjust() 方法     和     center()    方法

str.ljust(指定的长度,‘指定的字符’)     str.rjust(指定的长度,‘指定的字符’)     str.center(指定的长度,‘指定的字符’)    

ljust     指定一个长度使用指定的字符填充最后居左

rjust     指定一个长度使用指定的字符填充最后居右

center     指定一个长度使用指定的字符填充最后居中

mystr = "You got a dream"
mystr.ljust(50,'*') # You got a dream***********************************
mystr = 'You got a dream'
mystr.rjust(50,'*') # ***********************************You got a dream
mystr = "You got a dream"
mystr.center(50,'*') # *****************You got a dream******************

 

lstrip()   、   rstrip()     和      strip()  方法      

str.lstrip()      去除左边空格、换行符或者指定的字母    

str.rstrip()      去除右边空格、换行符或者指定的字母

str.strip()       去除两边的空格,换行古或者指定的字母

mystr = ' You got a dream '
mystr.rstrip() # 'You got a dream '
mystr.lstrip(' m') # ' You got a drea'
mystr.strip() # 'You got a drea'

 

partition()   和   rpartition()   方法

str.partition()          按照指定的字符切割字符串  返回一个元祖

str.rpartition()         从右边开始按照指定的字符  返回一个元祖

a = 'abcd'
print(a.partition('c'))    # ('ab','c','d')
a = 'abcdcd
print(a.rpartition('c'))   # ('abcd','c','d')

splitlines()   方法

str.splitilines()

按照  \r   \n  这个来分隔字符串    如果括号里面为false不包含换行符,如果括号中为true,保留换行符

mystr = 'You got a\n dream'
mystr.splitlines() # [' You got a', ' dream']
mystr.splitlines(True) # [' You got a\n', ' dream']

 

isalnum()  方法

str.isalnum()  

判断字符串是否只由数字和字母组成  返回布尔值

a = 'aaa3aaaa'
print(a.isalnum())  # true
a = 'aaa b sd 4'
print(a.isalnum())  # false   空格不是数字或字母

 

isdigit()   方法

str.isdigit()

判断字符串只有数字   返回布尔值

a = '234567890'
print(a.isdigit())  # true
a = 'sfsdf234324'
print(a.isdigit())  # false  

 

isalpha()  方法

str.isalpha()  

判断字符串是否由字母组成

a = 'jkdsafhdsklja'
print(a.isalpha())   # true
a = '3dfrdfs324'
print(a.isalpha())   # false

 

 isspace()  方法

str.isspace()

判断字符串是否由空格组成

a = '        '
print(a.isspace())   # true
a = 'dsafadsf     '
print(a.isspace())   # false

 

 join()    方法

str1.join(str2)

将指定的字符将序列化成一个新的字符串

a = 'a'
b = 'bbbbbbb'
print(a.join(b))    # babababababab

 

猜您喜欢

今日热门

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们