字符串
肉眼可以识别,具有特殊含义的字符组成串
一、 定义
-
直接赋值:
#单引号 >>> s1 = '字符串 1' #双引号 >>> s2 = "字符串 2" #三引号 >>> s3 = '''字符串 3'''
-
全局函数:str()
>>> s4 = str("字符串 4")
二、 常见方法
-
capitalize() # 让字符串首字母大写(规范化每段的第一句话)
>>> s1 'this is a string' >>> s1.capitalize() 'This is a string' >>> s1 'this is a string'
-
center(width,[fillchar]) # 设置字符串安装长度居中,fillchar默认是空格,可以自定义
>>> s1 'this is a string' >>> s1.center(50) ' this is a string ' >>> s1.center(50,"-") '-----------------this is a string-----------------' >>> s1 'this is a string'
-
ljust # 左对齐,fillchar默认是空格,可以自定义
>>> s1 'this is a string' >>> s1.ljust(20) 'this is a string ' >>> s1 'this is a string'
-
rjust # 右对齐,fillchar默认是空格,可以自定义
>>> s1 'this is a string' >>> s1.rjust(20) ' this is a string' >>> s1 'this is a string'
-
count() # 统计字符或者字符串出现的次数
>>> s1 'this is a string' >>> s1.count("i") 3
-
endswith() # 判断字符串是否以xxx结尾
>>> s1 'this is a string' >>> s1.endswith("th") False >>> s1.endswith("ing") True
-
startswith() # 判断字符串是否以xxx开头
>>> s1 'this is a string' >>> s1.startswith("this") True
-
index # 查找字符或者字符串在字符串中第一次出现的位置,如果字符或者字符串不存在,则抛出异常
>>> s1 'this is a string' >>> s1.index("i") 2
-
rindex # 查找字符或者字符串在字符串中最后一次出现的位置
>>> s1 'this is a string' >>> s1.rindex("i") 13
-
find # 查找字符或者字符串在字符串中第一次出现的位置,如果字符或者字符串不存在,则返回-1
>>> s1 'this is a string' >>> s1.find("s") 3 >>> s1.find("k") -1
-
rfind # 查找字符或者字符串在字符串中最后一次出现的位置
>>> s1 'this is a string' >>> s1.rfind("s") 10
-
encode(charset) # encode python3提供python用来将字符串转换为字节的一个方法
>>> s1 'this is a string' >>> s1.encode() b'this is a string' #python为了方便理解,吧字节翻译了 >>> s2 = s1.encode() >>> s2 b'this is a string' >>> type(s2) <class 'bytes'>
decode(charset) # 将字节转换为字符串
>>> s2 b'this is a string' >>> type(s2) <class 'bytes'> >>> s3 = s2.decode() >>> s3 'this is a string' >>> type(s3) <class 'str'>
-
format # 格式化字符串
>>> num 1 >>> type(num) <class 'int'> >>> print("{}".format(num)) 1
-
lower # 将字符串转换为小写字母
>>> st 'This is a string' >>> st.lower() 'this is a string' >>> st 'This is a string'
-
islower # 判断是否都是小写字母
>>> st 'This is a string' >>> st.islower() False
-
upper # 将字符串转换为大写字母
>>> st 'This is a string' >>> st.upper() 'THIS IS A STRING' >>> st 'This is a string'
-
isupper # 判断是否都是大写字母
>>> st 'THIS IS A STRING' >>> st.isupper() True
-
title # 将字符串转换为标题格式
>>> st 'This is a string' >>> st1 = st.title() >>> st 'This is a string' >>> st1 'This Is A String'
-
istitle # 判断字符串是否是标题
>>> st1 'This Is A String' >>> st1.istitle() True
-
isspace #判断是否是空格
>>> st 'This is a string' >>> st.isspace() False >>> st2 = " " >>> st2.isspace() True >>> st1[7].isspace() True
-
isdigit # 判断是不是数字
>>> st3 = "这是第3个字符串" >>> st3[3] '3' >>> st3[3].isdigit() True
-
isalnum # 判断是否都由有效符号
>>> st1 'This Is A String' >>> st1.isalnum() False >>> st5 = "abcd" >>> st5.isalnum() True >>> st4 'This ia a string!' >>> st4[16] '!' >>> st4[16].isalnum() False
-
isalpha # 判断是否都由字母组成
>>> st1 'This Is A String' >>> st1[:4] 'This' >>> st1[:4].isalpha() True >>> st1.isalpha() False
-
split(“符号”) # 按照特定的符号,将字符串切割,返回一个列表
>>> st1 'This Is A String' >>> st1.split(" ") ['This', 'Is', 'A', 'String'] >>> st1 'This Is A String' >>> st6 = st1.split(" ") >>> st6 ['This', 'Is', 'A', 'String']
-
join(iterable) # 按照特定的符号,将一个可迭代对象拼接成字符串
>>> st6 ['This', 'Is', 'A', 'String'] >>> " ".join(st6) 'This Is A String' >>> st7 = "-".join(st6) >>> st7 'This-Is-A-String'
-
strip # 清除字符串两侧的空格
>>> st8 = st1.center(50) >>> st8 ' This Is A String ' >>> st8.strip() 'This Is A String' >>> st8 ' This Is A String '
-
lstrip # 清除左侧空格
>>> st8 ' This Is A String ' >>> st8.rstrip() ' This Is A String'
-
rstrip # 清除右侧空格
>>> st8 ' This Is A String ' >>> st8.lstrip() 'This Is A String '
-
replace(“原字符串”, “新值”) # 替换对应的字符串
>>> st8 ' This Is A String ' >>> st8.replace(" ","*") '*****************This*Is*A*String*****************'
问题:怎么样切割一个字符串?
切片
python提供大家用来切割可迭代对象(容器)
方法:
- iterable[start:] # 从start位置开始切割,切到末尾
- iterable[start:end] # 从start位置开始切割,切到end位置为止,注意end并不包含 [)区间
- iterable[start:rear:step] # 从start位置截取,到rear位置结束,step为步长,步长默认是1
注意:
- 使用切片进行数据分割是时候,第三个参数是步长
- 如果步长为正,则从左到右切
- 如果步长为负,则从右向左切,但是下标还是从左而右数的
- 同时python也提供负索引(从右向左数,注意:最后一个元素是-1,倒数第二个是-2,以此类推)
>>> st1
'This Is A String'
>>> st1[5:]
'Is A String'
>>> st1[5:7]
'Is'
>>> st1[2:9:2]
'i sA'
>>> st1[-6:-1]
'Strin'
>>> st1[-6:-1:2]
'Srn'
练习题:
#根据完整的路径从路径中分离文件路径、文件名及扩展名
st = input("请输入一个文件路径:")
index1 = st.rfind(".")
index2 = st.rfind("\\")
add = st[:index2+1]
name = st[index2+1:index1]
rear = st[index1:]
print("路径:"+add)
print("文件名:"+name)
print("拓展名:"+rear)
#结果:
请输入一个文件路径:D:\Python\project\first.py
路径:D:\Python\project\
文件名:first
拓展名:.py