Python字符串的操作
一: python字符串的格式
双引号或者单引号中的数据都是字符串
a = 'str'
或者
a = "str"
二: 字符串的输出
name = "up_tech"
print("name: %s"%name)
三: 字符串的输入
userName = input("请输入用户名:")
print("用户名:%s" %userName)
四: 下标
列表与元组支持下标索引, 字符串实际就是字符串的数组,.
如果想去除部分字符,可以通过下标的方法获取
userName = "up_tech"
userName[2]
五:切片
切片是指对对象的截取其中一部分的操作. 字符串
, '列表','元组'都支持切片操作
切片的语法: [起始:结束:步长]
注意: 选取的区间属于左闭右开型, 即从"起始"位开始, 到"结束"位的前一位结束(不包含结束位本身)
name = "abcdef"
print(name[1:4]) # 取 下标1~3的字符
name = "abcdef"
print(name[0:5]) # 取 下标0~4的字符
name = "abcdef"
print(name[2:]) # 取 下标为2开始到最后的字符
name = "abcdef"
print(name[1:-1]) # 取 下标为1开始到最后第2个之间的字符
name = "abcdef"
print(name[:3]) # 取 下标为0~2的字符
name = "abcdef"
print(name[::2]) # 取 下标为所有的字符 从左边数起 步长为2
name = "abcdef"
print(name[1:5:2]) # 取 下标为1~4 步长为2
name = "abcdef"
print(name[::-2]) # 取 下标为所有的字符 从右边数起 步长为2
name = "abcdef"
print(name[5:1:-2]) # 取 下标为5~2数起 从右边数起 步长为2
六:字符串的常见操作
myStr = "hello world xz先生 up_tech xz先生"
########6.1: find
myStr.find(str, start=0, end=len(myStr))
检测str是否包含在 myStr中, 如果是返回开始的索引值, 否则返回 -1
>>> myStr = "hello world xz先生 up_tech xz先生"
>>> myStr.find("xz")
12
>>> myStr = "hello world xz先生 up_tech xz先生"
>>> myStr.find("xz", 0, len(myStr))
12
>>> myStr = "hello world xz先生 up_tech xz先生"
>>> myStr.find("xzx", 0, len(myStr))
-1
#######6.2: index
myStr.index(str, start=0, end=len(myStr))
跟着find()方法一样, 只不过如果str不在myStr中报一个异常
>>> myStr = "hello world xz先生 up_tech xz先生"
>>> myStr.index('xz', 0, len(myStr))
12
>>> myStr = "hello world xz先生 up_tech xz先生"
>>> myStr.index('xzx', 0, len(myStr))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
########6.3: count
返回str在start和end之间, 在myStr里面出现的次数
myStr.find(str, start=0, end=len(myStr))
>>> myStr = "hello world xz先生 up_tech xz先生"
>>> myStr.count('xz', 0, len(myStr))
2
>>> myStr = "hello world xz先生 up_tech xz先生"
>>> myStr.count('xxz', 0, len(myStr))
0
########6.4: replace
把myStr中的str1替换成str2,如果count指定,则替换不超过count次数
myStr.replace(str1, str2, myStr.count(str1))
>>> myStr = "hello world xz up_tech xz"
>>> myStr.replace('xz', 'zxz', myStr.count('xz'))
'hello world zxz up_tech zxz'
########6.5: split
以str为分隔符切片myStr, 如果maxSplit有指定值, 则仅分割maxSplit个字符串
myStr.split(str=" ", 2)
>>> myStr = "hello world xz up_tech xz"
>>> myStr.split(" ")
['hello', 'world', 'xz', 'up_tech', 'xz']
>>> myStr = "hello world xz up_tech xz"
>>> myStr.split(" ", 3)
['hello', 'world', 'xz', 'up_tech xz']
>>> myStr = "hello world xz up_tech xz"
>>> myStr.split(" ", 2)
['hello', 'world', 'xz up_tech xz']
########6.6: capitalize
把字符串的第一个字符大写
myStr.capitalize()
>>> myStr = "hello world xz up_tech xz"
>>> myStr.capitalize()
'Hello world xz up_tech xz'
########6.7: title
把字符串的每一个单词首字母大写
>>> myStr = "hello world xz up_tech xz"
>>> myStr.title()
'Hello World Xz Up_Tech Xz'
########6.8: startswitch
检查字符串是否是以obj开头, 是则返回True, 否则返回False
myStr.startswitch(obj)
>>> myStr = "hello world xz up_tech xz"
>>> myStr.startswith("hello")
True
>>> myStr = "hello world xz up_tech xz"
>>> myStr.startswith("Hello")
False
########6.9: endswith
检查字符串是否是以obj结束, 如果是返回True, 否则返回False
myStr.endswith(obj)
>>> myStr = "hello world xz up_tech xz"
>>> myStr.endswith("hello")
False
>>> myStr = "hello world xz up_tech xz"
>>> myStr.endswith("xz")
True
########6.10: lower: 转换myStr中所有大写字符为小写
myStr.lower()
>>> name = "XZ UP_tech"
>>> name.lower()
'xz up_tech'
########6.11: upper: 转换myStr中的小写字母为大写
myStr.upper()
>>> name = "up_tech xz"
>>> name.upper()
'UP_TECH XZ'
########6.12: ljust: 返回一个原字符串左对齐, 并使用空格填充值长度width的新字符串
myStr.ljust(width)
>>> name = "hello world"
>>> name.ljust(20)
'hello world '
########6.13: rjust: 返回一个原字符串右对齐, 并使用空格填充值长度width的新字符串
myStr.rjust(width)
>>> name = "hello world"
>>> name.rjust(20)
' hello world'
########6.14: center: 返回一个原字符串居中, 并使用空格填充值长度width的新字符串
myStr.center(width)
>>> name = "hello world"
>>> name.center(20)
' hello world '
########6.15: lstrip: 删除myStr左边的空白字符
myStr.lstrip()
>>> myStr = ' hello world xz up_tech xz'
>>> myStr.lstrip()
'hello world xz up_tech xz'
########6.16: rstrip: 删除myStr右边的空白字符
>>> myStr = ' hello world xz up_tech xz '
>>> myStr.rstrip()
' hello world xz up_tech xz'
########6.17: strip: 删除myStr两端的空白字符
>>> myStr = ' hello world xz up_tech xz '
>>> myStr.strip()
'hello world xz up_tech xz'
########6.18: rfind: 类似find()函数, 不过从右边开始查找
myStr.rfind(str, start=0, end=len(myStr))
>>> myStr = ' hello world xz up_tech xz '
>>> myStr.rfind("xz")
24
########6.19: rindex: 类似index(), 不过从右边开始
myStr.rindex(str, start=0, end=len(myStr))
>>> myStr = ' hello world xz up_tech xz '
>>> myStr.rindex("xz")
24
########6.20: partition: 把myStr以str分割成三部分, str前, str和str后
myStr.partition(str)
>>> name = "zhu up_tech xz"
>>> name.partition("up_")
('zhu ', 'up_', 'tech xz')
########6.21: rpartition: 类似partition, 不过从右边开始
myStr.rpartition(str)
>>> name = "zhu up_tech xz"
>>> name.rpartition("up_")
('zhu ', 'up_', 'tech xz')
########6.22: splitlines: 按照行分割, 返回一个包含各行作为元素的列表
myStr.splitline()
>>> name = "zhu up_tech xz"
>>> name.splitlines()
['zhu up_tech xz']
>>> name = "xz\nup_tech"
>>> name.splitlines()
['xz', 'up_tech']
########6.23: isalpha: 如果myStr所有的字符都是字母, 返回True, 否则返回False
myStr.isalpha()
>>> name = "xz\nup_tech"
>>> name.isalpha()
False
>>> name = "xzUpTehc"
>>> name.isalpha()
True
########6.24: isdigit: 如果myStr所有的字符都是数字, 返回True, 否则返回False
myStr.isdigit()
>>> name = "xzUpTehc"
>>> name.isdigit()
False
>>> name = "12343434"
>>> name.isdigit()
True
########6.25: isalnum: 如果myStr所有的字符都是数字或者字母, 返回True, 否则返回False
myStr.isalnum()
>>> name = "xz up_yec"
>>> name.isalnum()
False
>>> name = "12343434"
>>> name.isalnum()
True
######## 6.26: 如果myStr只包含空格, 则返回True, 否则返回False
>>> name = "xz up_tech xz"
>>> name.isspace()
False
>>> name = ' '
>>> name.isspace()
True
######## 6.27: 如果myStr中每个字符后面插入str, 构建出新的字符串
myStr.join(str)
>>> name = 'zhu'
>>> name.join(["my", "name"])
'myzhuname'
>>> name = 'zhu'
>>> name.join("xy")
'xzhuy'
网友评论