python视频笔记4 (字符串处理)

转义字符:将一些字符换成由特殊含义的字符

\n : 换行
\t : TAB 制表符
print (r"\\\t\\\") :如果字符中有太多的字符需要转义,那么可以用r同意内部的字符串默认不转义

eval():

功能:将字符串str当成有效的表达式来求值并返回计算结果
num1 = eval("123")
print (num1)		

len():

功能:返回字符串的长度(字符个数)
print (len("hello world"))

lower(str):

功能:转换字符串中的大写字母为小写字母
str10 = "HELLO world"
print (str10.lower())

upper():

功能:转换字符串中的大写字母为大写字母
str10 = "HELLO world" 
print (str10.upper())

swapcase():

功能:转换字符串中的大小转换
str10 = "HELLO world"
print (str10.swapcase())

caplitalize():

功能:转换字符串中的首字母大写
str10 = "HELLO world"
print (str10.caplitalize())

title():

功能:转换字符串中每个单词的首字母大写
str10 = "HELLO world"
print (str10.title())

center(width(宽度),fillchar(类型)):

功能:可做文章开头
print (str10.center(40,"*"))

ljust(width [fillchar(类型)] ):

功能:左对齐(默认是空格填充)
print (str10.ljust(49, "*"))

rjust(width[,fillchar(类型)]):

功能:右对齐
print (str10.rjust(49,"%"))	
zfill(width):
功能:右对齐(前面补0)
print (str10.zfill(40))

count(str10[,start][,end]):

功能:查找字符串中某个字符的个数
print (str10.count("hello", 1, len(str10)))

find(str[, start][, end]):

功能:查找字符串中某个字符是否存在,得到的是第一次出现的下标,没有返回-1
print(str10.find("world"))

rfind(str[, start][, end]):

功能:查找字符串中某个字符是否存在,得到的是第一次出现的下标,没有返回-1
print(str10.rfind("world"))

index(str, start=0 , end=len(str)):

功能:跟find一样只不过str如果不存在会报错
print(str10.index("good"))

rindex(str, start=0 , end=len(str)):

功能:跟find一样只不过str如果不存在会报错
print(str10.rindex("good"))

lstrip():

功能:截取掉字符串 中左侧的指定字符,默认字符为空格
print(str10.lstrip())

rstrip():

功能:截取掉字符串中右侧的指定字符,默认字符为空格
print(str10.rstrip())

strip():

功能:截取掉字符串中的指定字符,默认字符为空格
print(str10.strip())

split(str="",num):

以str为分隔符截取字符串,指定num 则截取num个字符串

str = "ni hao**********hell**world"
print(str.split("*",3))

eg:多少个单词

	c = 0
	for s in str.split("*")
		if len(s) > 0 
			c += 1 
		print (c)

splitlines():

按照(’\r’ | '\n '| ‘\r\n’) 分隔

str = '''
hello wordl 	
hello world 
hello world 
'''
print (str.splitlines(True)):默认为falus True 会保留换行符

join():

以一个特定的字符串作为分隔符将列表组合成一个字字符串

list = ['hello' 'world' 'hi' 'world']
str = " ".join(list)
print str

max() min()

str = "hello world z"
print (max(str))
print ("*" + min(str) + "*")

str.replace(“old” ,“new” [count]) :

将指定字符串替换,默认全部替换,也可以指定数量

print (str.replace("hello","hi"))

要转换的字符串 目标字符串

t =str.maketrans("ho" "65"):创建一个字符串映射表 h ---6  o ---5 
str = "hello hello hello world"
str1 = str.translate(t)
print (str1)

不常用字符串:

startwith(str,start=0 end=len(str)):

判断是否为str开头的字符串

str = “hello world”
print (str.startwith())

endwith(str,start=0 ,end=len(str)):

在给定的范围内,默认是整个字符串,判断是否为str结尾的字符串

print (startwith.endwith(str))
str.encode(encoding= "utf-8",errors="strict"):进行编码
data = str.encode("utf-8","ignore"):ignone错误不处理
print (date)
str = data.decode("utf-8"):进行解码
print (str)
注意:编码格式要与解码格式一致

isalpha():

如果字符串中至少有一个字符且所有字符都为字母返回TRUE 否则为FALSE

str = "hello world"
print (str.isalpha())

islnum():

如果字符串中至少有一个字符且所有字符都为字母或者数字返回TRUE否则FALSE

str = (1,2,3)
print (str.listNum())

isuppr():

如果字符串中至少有一个英文字符且所有字符都为大写英文字母返回TRUE否则FALSE

str = ("ABC",hello world)
print (str.isuppr)

islower():

如果字符串中至少有一个英文字符且所有字符都为小写英文字母返回TRUE否则FALSE

str = ("abc",HELLO )
print (str.islower())

istitle():
如果字符串是标题化的,返回TRUE,否则FALSE

print("Hello World" .istitle())

isdigit():

如果字符只包含数字字符,则返回TRUE

print("123".isdigit())

isnumberic():

如果字符只包含数字字符,则返回TRUE

print("1,2,3,4".isnumberic())

isdecimal():

如果字符只包含十进制字符串,则返回TRUE

print("123Z".isdecimal())

isspace():

如果字符串中只包含空格则返回TRUE

print ("\t".isspace)
print ("\n".isspace)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值