字符串方法

#字符串方法
#1.capitalize()方法:把首字符转换为大写

txt = "hello,and welcome to my world"
x = txt.capitalize()#第一个字符变大写,如果第一个字符是数字的话就不变
print(x)

#2.casefold()方法:把字符串转换为小写,和lower()方法相似,比lower()强大

txt = "Hello,And Welcome To My World!"
x = txt.casefold()
print(x)

#3.center()方法:返回居中的字符串

txt = "banana"
x = txt.center(20)#打印单词 "banana",占用 20 个字符(包含banana),并使 "banana" 居中:
print(x)
txt = "banana"
x = txt.center(20,"O")#使用字母”O"作为填充字符
print(x)

#4.count()方法:返回指定值在字符串中出现的次数

txt = "I love apples,apple are my favorite fruit"
x = txt.count("apple")
print(x)
txt = "I love apples,apple are my favorite fruit"
x = txt.count("apple",10,24)#从位置10到24进行检索
print(x)

#5.endswith()方法:如果字符串以指定值结尾,则返回true

#6.expandtabs()方法:将制表符的大小设置为指定的空格数

txt = "H\te\tl\tl\to"
x = txt.expandtabs(2)
print(x)
print(txt)#默认是4
print(txt.expandtabs(4))
print(txt.expandtabs(10))

#7.find()方法:在字符串中搜索指定的值并返回它被找到的位置

txt = "Hello,welcome to my world."
x = txt.find("welcome")
print(x)#6
txt = "Hello,welcome to my world."
x = txt.find("e",5,10)#只搜索5到10时,字母“e"在文本中首次出现的位置
print(x)

#如果找不到该值,则find()方法返回-1,但是index()方法将引发异常

txt = "Hello,welcome to my world."
print(txt.find("q"))
#print(txt.index("q"))

#8.format()方法:使用指定的格式

txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))#49.00
#使用不同的占位符值:
txt1 = "My name is {fname},I'am {age}".format(fname = "Bill",age = 64)
txt2 = "My name is {0},I'am {1}".format("Bill",64)
txt3 = "My name is {},I'am {}".format("Bill",64)
print(txt1)
print(txt2)
print(txt3)
txt = "You scored {:%}"
print(txt.format(0.25))

txt = "You scored {:.0%}"
print(txt.format(0.25))

#9.isalnum()方法:如果字符串中的所有字符都是字母数字,则返回True

txt = "Company12"
x = txt.isalnum()
print(x)

#10.isalpha()方法:如果字符串中所有字符都在字母表中,则返回True
#如果所有字符都是字母(a-z),则isalpha()方法将返回True
#非字母的字符例子:(space)!#%&?等等

txt = "Company10"
x = txt.isalpha()
print(x)#false

#11.isdecimal():如果字符串中的所有字符都是小数,则返回TRUE.此方法用于unicode对象

txt = "\u0047"#unicode for G
x = txt.isdecimal()
print(x)#false

#12.isdigit():如果字符串中的所有字符都是数字,则返回True

b = "\u00B2"#unicode for ²
print(b.isdigit())#指数也被视作数字

#13.isidentifier()方法:如果字符串是标识符,则返回True
#如果字符串仅包含字母数字字母(a-z)和(0-9)或下划线(_),则该字符串被视为有效标识符。有效的标识符不能以数字开头或包含任何空格。

#14.islower():如果字符串中的所有字符都是小写,则返回True

#15.isnumeric():如果字符串中所有字符都是数,则返回True
#指数也被视作数字

#16.isprintable()方法:所有字符是否可打印,不可打印的字符可以是回车和换行符

txt = "Hello!\nAre you #1?"#\n是换行
x = txt.isprintable()#false
print(x)

#17.isspace():所有字符是否都是空格

#18.istitle():如果文本中所有的单词均以大写字母开头,而单词的其余部分均为小写字母,则返回True,符合和数字将被忽略

a = "HELLO, AND WELCOME TO MY WORLD"
b = "Hello"
c = "22 Names"
d = "This Is %'!?"

print(a.istitle())#false
print(b.istitle())#true
print(c.istitle())#true
print(d.istitle())#true

#19.isupper():如果字符串中的所有字符都是大写,则返回True
#不检查数字、符号和空格,仅检查字母字符

#20.join():把可迭代对象的元素连接到字符串的末尾。

myTuple = ("Bill","Steve","Elon")
x = "#".join(myTuple)
print(x)# Bill#Steve#Elon

myDict = {"name": "Bill", "country": "USA"}
mySeparator = "TEST"

x = mySeparator.join(myDict)

print(x)#nameTESTcountry

#21.ljust():返回字符串的左对齐版本

txt = "banana"
x = txt.ljust(20)#返回 20 个字符长的 "banana" 一词的左对齐版本."banana" 一词的右边实际上有 14 个空格。
print(x,"is my favorite fruit.")
#使用字母”O“作为填充字符
txt = "banana"
x = txt.ljust(20, "O")
print(x)

#22.lstrip():返回字符串的左修剪版本

txt = "     banana     "
x = txt.lstrip()
print("of all fruits", x, "is my favorite")

txt = ",,,,,ssaaww.....banana"
x = txt.lstrip(",.asw")#删除前导字符
print(x)#banana

#23.partition():返回元组,其中的字符串被分为三部分

txt = "I could eat bananas all day"
x = txt.partition("bananas")
print(x)

#如果找不到指定的值,则 partition() 方法将返回一个元组,其中包含:1 - 整个字符串,2 - 空字符串,3 - 空字符串

txt = "I could eat bananas all day"
x = txt.partition("apples")
print(x)

#24.replace():返回字符串,其中指定的值被替换为指定的值

txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x)

#替换所有出现的单词 “one”:

txt = "one one was a race horse, two two was one too."
x = txt.replace("one", "three")
print(x)

#替换前两次出现的单词 “one”:

txt = "one one was a race horse, two two was one too."
x = txt.replace("one", "three", 2)
print(x)

#25.rfind() 方法查找指定值的最后一次出现。
#如果找不到该值,则 rfind() 方法将返回 -1。
#rfind() 方法与 rindex() 方法几乎相同

txt = "Hello, welcome to my world."
x = txt.rfind("e")
print(x)

#如果只在位置 5 和位置 10 之间搜索,文本中最后出现的字母 “e” 在何处?

txt = "Hello, welcome to my world."
x = txt.rfind("e", 5, 10)
print(x)

#26.rjust():返回字符串的右对齐版本
#使用字母 “O” 作为填充字符:

txt = "banana"
x = txt.rjust(20, "O")
print(x)

#27.rstrip():返回字符串的右边修剪版本

#28.split():在指定的分隔符处拆分字符串,并返回列表

txt = "welcome to China"
x = txt.split()
print(x)#['welcome', 'to', 'China']

txt = "apple#banana#cherry#orange"

将 max 参数设置为 1,将返回包含 2 个元素的列表!

x = txt.split("#", 1)
print(x)

#29.splitlines()方法:将字符串拆分为列表。拆分在换行符处完成。

txt = "Thank you for your visiting\nWelcome to China"
x = txt.splitlines()#将字符串拆分为一个列表,其中每一行都是一个列表项
print(x)
txt = "Thank you for your visiting\nWelcome to China"
x = txt.splitlines(True)#拆分字符串,但保留换行符
print(x)

#30.startwith():如果以指定值开头的字符串,则返回 true。

txt = "Hello, welcome to my world."
x = txt.startswith("Hello")
print(x)
txt = "Hello, welcome to my world."
x = txt.startswith("wel", 7, 20)#检查位置 7 到 20 是否以字符 "wel" 开头:
print(x)

#31.strip()返回字符串的剪裁版本
#strip() 方法删除任何前导(开头的空格)和尾随(结尾的空格)字符(空格是要删除的默认前导字符)

txt = "     banana     "
x = txt.strip()
print("of all fruits", x, "is my favorite")
txt = ",,,,,rrttgg.....banana....rrr"
x = txt.strip(",.grt")
print(x)

#32.swapcase()方法返回一个字符串,将小写字母大写,大写字母小写

txt = "Hello My Name Is Elon"
x = txt.swapcase()
print(x)

#33.title()把每个单词的首字符转换为大写

txt = "Welcome to my world"
x = txt.title()
print(x)

#非字母字母之后的第一个字母将转换为大写字母

txt = "hello d2d2d2 and 5g5g5g"
x = txt.title()
print(x)

#34.zfill():在字符串的开头添加零(0),直到达到指定的长度。

a = "hello"
b = "welcome to my world"
c = "10.000"

print(a.zfill(10))
print(b.zfill(10))
print(c.zfill(10))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值