python中string字符串

'''什么是字符串
字符串是以单引号或双引号括起来的任意文本
'aa' "ddd"
字符串是不可变的

'''

str1="a"
str2="b"
str3=str1+str2
print("str3=",str3)
#输出重复字符串
str4="good"
str5=str4*3
print("str5=",str5)

#访问字符串中的某个字符
#通过索引下标查找字符,索引从0开始
#字符串名[下标]
str6="ong is a boy"
print(str6[2])
#str6[1]="a"会报错,字符串不可变
print("str6=",str6)

#截取字符串中的一部分
str7="ong is a boy"
#从4开始到7之前
str8=str7[4:7]
#从头截取到给定下标之前
str9=str7[:7]
#从给定下标处截取到结尾
str10=str7[7:]
print("str8=",str8)
print("str9=",str9)
print("str10=",str10)


str7="ong is a boy"
#判断boy在不在这个字符串中,在则出现true,不在则False
print("boy"in str7)
print("good" not in str7)


#格式化输出
print("ang is a good time")
str7="ong is a boy"
num=10
f=5.22313
# %d(整数站位符) %s(字符串站位符) %f(浮点数站位符)
# %f默认小数点后6位,%.3f精确到小数点后3位。默认会四舍五入
print("num=",num,"f=",f)
print("num= %d,str7=%s,f=%.9f" %(num,str7,f))
'''
#转义字符 \
将一些字符转换成有特殊含义的字符
'''
# \n换行符,
print("num= %d\nstr7=%s\nf=%.9f" %(num,str7,f))
print("ong is a good \team")#显示ong is a good \team
#如果要没特殊含义出现都是两\\出现的
print("ong is a good \\n team")#ong is a good \n team
# \' \"
#print('tom is a 'good'man')#这样会报出
print('tom is a \'good\'man')
print("tom is a 'good'man")#双引号里面可以有单引号
print("tom is a \"good\"man")
#如果多行转换可以用3个单引号或者3个双引号
print("""
ong
is
good
team
""")
# \t 制表符 是4个空格
print("tom\t girl")
#如果字符中有很多字符串都需要转义,就需要加入很多\,为了化简
#python允许用r表示内部的字符串默认不转义
print(r"C:\Users\Administrator\Desktop\学习")#加r打印出C:\Users\Administrator\Desktop\学习



#eval(str)
#功能:将字符串str当成有效的表达式来求值并返回计算结果
num1=eval("123")
print(num1)#结果123
print(type(num1))#<class 'int'>eval把字符串变为整数类型,跟int方式相同
print(eval("+123"))#123
print(eval("12+3")) #15
print(int(12+3)) #15
#print(eval("12ad")) #出错


#len(str)
#返回字符串的长度(字符个数)
print(len("ong is a good"))

#lower()转换字符串中大写字母为小写字母
#格式:str.lower()
str15="ONG IS a good"
print(str15.lower()) #ong is a good
print("str15=%s" %(str15))

#upper(str)转换字符串中小写字母为大写字母
str15="ONG IS a good"
print(str15.upper()) # ONG IS A GOOD
#swapcase() 转换字符串中小写字母为大写字母,大写字母变小写字母
str15="ONG IS a good"
print(str15.swapcase()) #ong is A GOOD

#capitalize()首字母大写,其他小写
str15="ONG IS a good"
print(str15.capitalize()) #Ong is a good

#title()每个单词的首字母大写,其他变小写
str15="ONG IS a good"
print(str15.title()) # Ong Is A Good

#center(width,fillchar)居中对齐,width为 要求宽度,fillchar是填充字符串
str15="ONG IS a good"
print(str15.center(40,"*"))#*************ONG IS a good**************

#ljust(width[,fillchar])左对齐,fillchar是填充的字符
str15="ONG IS a good"
print(str15.ljust(40))#ONG IS a good
print(str15.ljust(40),"*") #ONG IS a good *
print(str15.ljust(40,"*"))#ONG IS a good***************************

#rjust(width,fillchar)右对齐

str1="a"
print(ord(str1))#将小写a转为ascii值,得到ascii值为97
str2=65
print(chr(str2))#找出数字中对应的字符


#find(str[,start][,end])
str30="ONG IS a low team"
print(str30.find("low"))#结果为 9
print(str30.find("low",1,len(str30)))#9

#rfind(str[,start][,end])
str30="ONG IS a low team"
print(str30.rfind("low"))#9
print(str30.rfind("low",0,15))#9

#index(str,start=0,end=len(str))
#根find()一样,只不过如果str不存在的时候会报错
str31="ONG IS a low team"
#print(str31.index("good"))

#rindex(str,start=0,end=len(str))
#start默认为0,end默认为字符长度
#跟rfind()一样,只是不存在会报错
str32="ONG IS a low team"
print(str32.rindex("low"))


#lstrip()截掉支付串左侧指定的字符,默认为空格

str33="*** ONG IS a low team"
print(str33.lstrip("*"))


#rstrip()截掉支付串右侧指定的字符,默认为空格

str34="*** ONG IS a low team***"
print(str34.rstrip("*"))

#strip() 去掉两边的字符
str35="*** ONG IS a low team***"
print(str35.strip("*"))


str36="a"
print(ord(str36)) #97
str37=chr(65)
print(str37) #A

#split(str="",num) 38#
#以str为分隔符截取字符串,指定num,则截取num个字符
str38="ong*is** a *****good***team"
print(str38.split("*",3))#['ong', 'is', '', ' a *****good***team']
print(str38.split("*"))#['ong', 'is', '', ' a ', '', '', '', '', 'good', '', '', 'team']
#计算中间有多少个单词
str38="ong*is** a *****good***team"
list39=str38.split("*")#根据*号来截取字符串
print(list39)#['ong', 'is', '', ' a ', '', '', '', '', 'good', '', '', 'team']
c=0
for s in list39:
if len(s) > 0:#按list39中截取的字符串长度判断
c+=1
print(c)

#splistlines([keepends]) 按照('/r','/r/n','\n')分割,返回
#keepends==True 会保留换行符 False则不会
str40='''
ong is a good team
dafa dfasdf fadfaf adf
fjjljkad
adfjkj lj;;laewejl efakjd

'''
#['\n', 'ong is a good team\n', 'dafa dfasdf fadfaf adf\n', 'fjjljkad\n', 'adfjkj lj;;laewejl efakjd\n', '\n']
print(str40.splitlines(True))#保留换行符
#['', 'ong is a good team', 'dafa dfasdf fadfaf adf', 'fjjljkad', 'adfjkj lj;;laewejl efakjd', '']
print(str40.splitlines(False))#都是按行截取的



#join(seq) 以指定的支付串分割符,将seq中的所有元素组合成
#一个字符串
list41=['ong','is','a','good','team']
str42="*".join(list41)#ong*is*a*good*team
print(str42)#

#max() min()
str43=" ong is a good x"
print(max(str43))# x最大的
print('*'+min(str43)+'*')#* *最小的事空格


#replace(oldstr,newstr,coumt)
#用newstr替换oldstr,默认是全部替换,如果指定了count,那
# 只替换count之前个么
str44="ong is a good good good team"
str45=str44.replace("good","nice",1)#ong is a nice good good team
print(str44)
print(str45)

#创建一个字符串映射表
print("****")
#maketrans("a","2")将字符串中的a替换成2
str46=str.maketrans("on","23")#23g is a g22d g22d g22d team
#将a替换为2,n替换为3.
str47="ong is a good good good team"
str48=str47.translate(str46)#执行替换
print(str48)


#startswith(str,start=0,end=len(str))
#在给定的范围内判断是否是以给定的字符串开头,如果没
# 指定范围,默认整个字符串
str49="ong is a good good good team"
print(str49.startswith("ong",))#True ong在字符串中是开头



#endswith(str,start=0,end=len(str))
#在给定的范围内判断是否是以给定的字符串开头,如果没
# 指定范围,默认整个字符串
str49="ong is a good good good team"
print(str49.endswith("ong",))#False ong不是结尾


#编码
#encode(encoding="utf-8",errors="strict")
str51="ong is a good team将"
#ignore如果出错不处理
data52=str51.encode("utf-8","ignore")#b'ong is a good team'
print(data52)
print(type(data52))

#解码 decode 要主要解码要与编码格式一致
str53=data52.decode("gbk","ignore")#ong is a good team灏
str53=data52.decode("utf-8","ignore")#ong is a good team将
print(str53) #ong is a good team

#isalpha() 字母
#如果字符串中至少有一个字符且所以的字符都是字母,返回True,否
# 则返回False
str54="dfadadsfad"
print(str54.isalpha())#True

#isalnum() 字母或数据
#如果字符串中至少有一个字符且所有的字符都是字母或者数据,返回True,否
# 则返回False
str54="dfad556ad2sfad"
print(str54.isalnum())#True


#isuper()大写字母
#如果字符串中至少有一个大写英文字符且所有的字符都是大写字母或者符号数字返回True,
#否则返回False
print("ABC".isupper())#True
print("1".isupper())#False
print("ABC1".isupper())#True
print("ABC#".isupper())#True


#islower() 小写字母
#如果字符串中至少有一个小写英文字符且所有的字符都是小写字母或者符号数字返回True,
#否则返回False
print("abc".islower())#True
print("abcA".islower())#False
print("1".islower())#False
print("abc1".islower())#True
print("abcc#".islower())#True


#istitle()
#4如果字符串是标题化的返回True,否则返回False
#每个单词的首字母为大写时为标题化
print("Ong a".istitle())#False
print("Ong A".istitle())#True

#isdigit()
#isnumeric()
#如果字符串中只包含数字字符返回True,否则返回False
print("123".isdigit())#True
print("123a".isdigit())#False
print("123".isnumeric())#True
print("123a".isnumeric())#False

#isdecimaal()字符串中只包含10进制字符
print("123".isdecimal())
print("123a".isdecimal())


#isspace()
#如果字符串中只包含空格则返回True,否则返回False
print(" ".isspace())#True
print("\t".isspace())#True
print("\n".isspace())#True
print("\r".isspace())#True



str33="*** ONG IS a low team"
print(str32.lstrip("*"))

转载于:https://www.cnblogs.com/zlong123/p/10418636.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值