转载请注明预见才能遇见的博客:http://my.csdn.net/
原文地址:https://blog.csdn.net/pcaxb/article/details/90677077
人工智能系列-Python系列(三)字符串详解
目录
1 find 从开始往后找,找到第一个之后,获取其位置,未找到 -1
2 format 和 format_map 格式化,将一个字符串中的占位符替换为指定的值
3 isspace 判断是否全部是空格,说明:不是判断是不是空字符串
8 isupper、upper、islower、lower转换为大小写
2 所有变小写,lower 和 casefold, casefold更牛逼,很多未知的对相应变小写
3 设置宽度,并将内容居中 20 代指总长度 center 和 ljust 和 rjust 和 zfill
5 以什么什么结尾 以什么什么开始 startswith和 endswith
9 isdecimal、isdigit、isnumeric当前输入是否是数字
11 istitle 、title、istitle判断是否是标题
12 islower、lower、isupper、upper判断是否全部是大小写 和 转换为大小写
13 lstrip、rstrip、strip移除指定字符串 有限最多匹配
16 splitlines 分割,只能根据,true,false:是否保留换行
17 startswith、endswith以xxx开头,以xx结尾
19 isidentifier 字母,数字,下划线 : 标识符 def class
20 练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置
四、字符串格式化,百分号方式和 Format方式,字符串拼接
一、10个常用字符串函数和总结
1 find 从开始往后找,找到第一个之后,获取其位置,未找到 -1
test = "alexalex"
v = test.find('ex')
print(v) # 2
2 format 和 format_map 格式化,将一个字符串中的占位符替换为指定的值
test = 'i am {name}, age {a}'
print(test) # i am {name}, age {a}
v = test.format(name='alex', a=19)
print(v) # i am alex, age 19
test1 = 'i am {0}, age {1}'
print(test1) # i am {0}, age {1}
v1 = test1.format('alex', 19)
print(v1) # i am alex, age 19
test = 'i am {name}, age {a}'
v1 = test.format(name='df', a=10)
v2 = test.format_map({"name": 'alex', "a": 19})
3 isspace 判断是否全部是空格,说明:不是判断是不是空字符串
test = ""
v = test.isspace()
print(v) # False
4 join将字符串中的每一个元素按照指定分隔符进行拼接
test = "你是风儿我是沙"
v = "_".join(test)
print(v) # 你_是_风_儿_我_是_沙
5 split 分割为指定个数
test = "testasdsddfg"
v = test.split('s', 2)
print(v) # ['te', 'ta', 'dsddfg']
v2 = test.rsplit()
print(v2) # ['testasdsddfg']
6 replace 将指定字符串替换为指定字符串
test = "alexalexalex"
v1 = test.replace("ex", 'bbb')
print(v1) # albbbalbbbalbbb
v2 = test.replace("ex", 'bbb', 2)
print(v2) # albbbalbbbalex
7 strip 移除指定字符串 有限最多匹配
test = "xacchfahxahhhxa"
v3 = test.strip('xa')
print(v3) # cchfahxahhh
# 去掉收尾的指定字符、空格和回车
# strip 去掉收尾的空格和回车
name = '\npca\n \n'
print(name.strip()) # pca
# strip 去掉首尾指定字符串,中间不能去
name2 = '***pca*11*****'
print(name2.strip('*')) # pca*11
8 isupper、upper、islower、lower转换为大小写
test = "Alex"
v3 = test.isupper()
v4 = test.upper() # 大写
print(v3, v4) # False ALEX
v1 = test.islower()
v2 = test.lower() # 小写
print(v1, v2) # False alex
9 range 获取连续或不连续的数字,
Python2中直接创建在内容中, python3中只有for循环时,才一个一个创建
r1 = range(10)
r2 = range(1, 10)
# 帮助创建连续的数字,通过设置步长来指定不连续
r3 = range(1, 10, 2)
v = range(0, 100, 5)
print(r1, r2, r3, v) # range(0, 10) range(1, 10) range(1, 10, 2) range(0, 100, 5)
for item in v:
print(item)
10 字符串一旦创建,不可修改
# 一旦修改或者拼接,都会造成重新生成字符串
# name = "zhengjianwen"
# age = "18"
# info = name + age
# print(info)
二、4个for循环、索引、切片、len总结
1、for循环
# for循环
# for 变量名 in 字符串:
# 变量名
# break
# continue
test = "郑建文妹子有种冲我来"
# for zjw in test:
# print(zjw) # 单个字打印
for zjw in test:
print(zjw) # 郑
break
for name in test:
continue
print(name)
2、索引,下标,获取字符串中的某一个字符
test = "郑建文妹子有种冲我来"
v = test[3] # 妹
print(v)
3、切片 字符串截取
test = "郑建文其他处理函数哈哈哈"
v1 = test[4] # 他
v = test[0:5] # 郑建文其他 0=< <5
print(v, v1)
4、获取长度
Python3: len获取当前字符串中由几个字符组成
test = "123456789"
v = len(test)
print(v) # 9
三、20个其他字符串函数和总结
1 首字母大写 capitalize
test = "aLex"
v = test.capitalize() # Alex
print(v)
2 所有变小写,lower 和 casefold, casefold更牛逼,很多未知的对相应变小写
v1 = test.casefold() # alex
print(v1)
v2 = test.lower() # alex
print(v2)
3 设置宽度,并将内容居中 20 代指总长度 center 和 ljust 和 rjust 和 zfill
# 空白未知填充,一个字符,可有可无
test = "alex"
v3 = test.center(20, "中")
print(v3) # 中中中中中中中中alex中中中中中中中中
v4 = test.ljust(20, "*")
print(v4) # alex****************
v5 = test.rjust(20, "*")
print(v5) # ****************alex
v6 = test.zfill(20)
print(v6) # 0000000000000000alex
4 去字符串中寻找,寻找子序列的出现次数 count
test = "aLexalexr"
v = test.count('ex')
print(v) # 2
v1 = test.count('ex', 5, 6)
print(v1) # 0
5 以什么什么结尾 以什么什么开始 startswith和 endswith
test = "alex"
v1 = test.endswith('ex')
v2 = test.startswith('ex')
print(v1) # True
print(v2) # False
6 expandtabs,断句20,
test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
v = test.expandtabs(20)
print(v)
7 isalnum 字符串中是否只包含 字母和数字
test = "123"
v = test.isalnum()
print(v) # True
8 isalpha 是否是字母,汉子
test = "as2df"
v = test.isalpha()
print(v) # False
9 isdecimal、isdigit、isnumeric当前输入是否是数字
test = "二" # 1,②
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1, v2, v3)
10 isprintable 是否存在不可显示的字符
test = "oiuas\tdfkj"
v = test.isprintable()
print(v) # False
11 istitle 、title、istitle判断是否是标题
test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)
12 islower、lower、isupper、upper判断是否全部是大小写 和 转换为大小写
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1, v2) # False alex
v3 = test.isupper()
v4 = test.upper()
print(v3, v4)
13 lstrip、rstrip、strip移除指定字符串 有限最多匹配
test = "xa"
# v1 = test.lstrip('xa')
# print(v1) # xa
# v2 = test.rstrip('9lexxexa')
# print(v2) # xa
v3 = test.strip('xa')
print(v3)
# 去除左右空白、去除\t \n
# v4 = test.lstrip()
# v5 = test.rstrip()
# v6 = test.strip()
14 maketrans、translate对应关系替换
test = "aeiou"
test1 = "12345"
v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "12345")
new_v = v.translate(m)
print(new_v)
15 partition、rpartition分割为三部分
test = "testasdsddfg"
v = test.partition('s')
print(v)
v2 = test.rpartition('s')
print(v2)
16 splitlines 分割,只能根据,true,false:是否保留换行
test = "asdfadfasdf\nasdfasdf\nadfasdf"
v = test.splitlines(False)
print(v) # ['asdfadfasdf', 'asdfasdf', 'adfasdf']
17 startswith、endswith以xxx开头,以xx结尾
test = "backend 1.1.1.1"
v = test.startswith('a')
print(v) # False
test.endswith('a')
18 swapcase 大小写转换
test = "aLex"
v = test.swapcase()
print(v) # AlEX
19 isidentifier 字母,数字,下划线 : 标识符 def class
a = "def"
v = a.isidentifier()
print(v) # True
20 练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置
# 方法一:
# input_value = input(">>>")
# count = 0
# for v in input_value:
# print(count, v)
# count += 1
# 方法二:
input_value = input(">>>")
length = len(input_value)
rangeLen = range(0, length)
for rl in rangeLen:
print(rl, input_value[rl])
四、字符串格式化,百分号方式和 Format方式,字符串拼接
1.百分号方式
%[(name)][flags][width].[precision]typecode
- (name) 可选,用于选择指定的key
- flags 可选,可供选择的值有:
- + 右对齐;正数前加正好,负数前加负号;
- - 左对齐;正数前无符号,负数前加负号;
- 空格 右对齐;正数前加空格,负数前加负号;
- 0 右对齐;正数前无符号,负数前加负号;用0填充空白处
- width 可选,占有宽度
- .precision 可选,小数点后保留的位数
- typecode 必选
- s,获取传入对象的__str__方法的返回值,并将其格式化到指定位置
- r,获取传入对象的__repr__方法的返回值,并将其格式化到指定位置
- c,整数:将数字转换成其unicode对应的值,10进制范围为 0 <= i <= 1114111(py27则只支持0-255);字符:将字符添加到指定位置
- o,将整数转换成 八 进制表示,并将其格式化到指定位置
- x,将整数转换成十六进制表示,并将其格式化到指定位置
- d,将整数、浮点数转换成 十 进制表示,并将其格式化到指定位置
- e,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(小写e)
- E,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(大写E)
- f, 将整数、浮点数转换成浮点数表示,并将其格式化到指定位置(默认保留小数点后6位)
- F,同上
- g,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是e;)
- G,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是E;)
- %,当字符串中存在格式化标志时,需要用 %%表示一个百分号
tpl = "i am %s" % "alex"
tpl = "i am %s age %d" % ("alex", 18)
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
tpl = "percent %.2f" % 99.97623
tpl = "i am %(pp).2f" % {"pp": 123.425556, }
tpl = "i am %.2f %%" % {"pp": 123.425556, }
2.format方式
[[fill]align][sign][#][0][width][,][.precision][type]
- fill 【可选】空白处填充的字符
- align 【可选】对齐方式(需配合width使用)
- <,内容左对齐
- >,内容右对齐(默认)
- =,内容右对齐,将符号放置在填充字符的左侧,且只对数字类型有效。 即使:符号+填充物+数字
- ^,内容居中
- sign 【可选】有无符号数字
- +,正号加正,负号加负;
- -,正号不变,负号加负;
- 空格 ,正号空格,负号加负;
- # 【可选】对于二进制、八进制、十六进制,如果加上#,会显示 0b/0o/0x,否则不显示
- , 【可选】为数字添加分隔符,如:1,000,000
- width 【可选】格式化位所占宽度
- .precision 【可选】小数位保留精度
- type 【可选】格式化类型
- 传入” 字符串类型 “的参数
- s,格式化字符串类型数据
- 空白,未指定类型,则默认是None,同s
- 传入“ 整数类型 ”的参数
- b,将10进制整数自动转换成2进制表示然后格式化
- c,将10进制整数自动转换为其对应的unicode字符
- d,十进制整数
- o,将10进制整数自动转换成8进制表示然后格式化;
- x,将10进制整数自动转换成16进制表示然后格式化(小写x)
- X,将10进制整数自动转换成16进制表示然后格式化(大写X)
- 传入“ 浮点型或小数类型 ”的参数
- e, 转换为科学计数法(小写e)表示,然后格式化;
- E, 转换为科学计数法(大写E)表示,然后格式化;
- f , 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
- F, 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
- g, 自动在e和f中切换
- G, 自动在E和F中切换
- %,显示百分比(默认显示小数点后6位)
- 传入” 字符串类型 “的参数
tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
3.字符串拼接
# 1.字符串拼接
# 1) + 号拼接字符串
msg = "username Age" + "-12"
print(msg)
username = "cc"
msg = "username=" + username + " Age=" + str(12)
print(msg) # username=cc Age=12
# 2)%s 号拼接字符串 s 代表字符串,但是可以传数字、列表等,, %d只能是数字,%f是浮点数
msg1 = "ab %s dem" % "m" # ab m
print(msg1)
msg2 = 'a %s c %s ' % ("b", 1)
print(msg2) # a b c 1
人工智能系列-Python系列(三)字符串详解