(三)Python学习之字符串常用操作(上)

一、字母处理

  1. upper(): 全部大写;
  2. lower(): 全部小写;
  3. swapcase(): 大小写互换;
  4. capitalize(): 首字母大写,其余小写;
  5. title(): 首字母大写(转换为标题);
  6. casefold(): 全部小写(很多未知的对应变小写)
test1 = "helloWorld"
print(test1.upper())
print(test1.lower())
print(test1.swapcase())
test2 = "hello World"
print(test2.capitalize())
print(test2.title())

(三)Python学习之字符串常用操作(上)

S1 = "HeLLo"
S2 = "ß"  # 德语,"ß"正确的小写是"ss"
print(S1.lower())
print(S1.casefold())
print(S2.lower())
print(S2.casefold())

(三)Python学习之字符串常用操作(上)
二、格式化相关

  1. ljust(width, fillchar = None): 获取固定长度,左对齐,右边不够用fillchar补齐;当fillchar为空,用空格补齐;
  2. rjust(width, fillchar = None): 获取固定长度,右对齐,左边不够用fillchar补齐;当fillchar为空,用空格补齐;
  3. center(width, fillchar = None): 获取固定长度,中间对齐,两边不够用fillchar补齐;当fillchar为空,用空格补齐;
  4. zfill(width): 获取固定长度,右对齐,左边不足用0补齐;
  5. expandtabs(tabsize = None): 把字符串中的 tab 符号(’\t’)转换为tabsize个空格;当tabsize为空时,转换为0个空格;tab 符号(’\t’)默认的空格数是 8;
a = "11111"
print(a.ljust(10, "*"))
print(a.ljust(10))
print(a.rjust(10, "*"))
print(a.rjust(10))
print(a.center(10, "*"))
print(a.center(10))
print(a.zfill(10))

(三)Python学习之字符串常用操作(上)

str = "this is\tstring example....wow!!!"
print(str)
print(str.expandtabs())
print(str.expandtabs(16))

(三)Python学习之字符串常用操作(上)
三、字符串搜索相关

  1. find(sub, start = None, end = None): 从左到右在指定范围(大于等于start小于end)内搜索指定字符串sub,找到第一个之后,返回其位置,没有返回-1;无指定范围则表示搜索整个字符串;
  2. index(sub, start = None, end = None): 从左到右在指定范围(大于等于start小于end)内搜索指定字符串sub,找到第一个之后,返回其位置,没有程序报错;无指定范围则表示搜索整个字符串;
  3. rfind(sub, start = None, end = None):从右到左在指定范围(大于等于start小于end)内搜索指定字符串sub,找到第一个之后,返回其位置,没有返回-1;无指定范围则表示搜索整个字符串;
  4. count(sub,start = None,end = None): 在指定范围(大于等于start小于end)内统计指定的字符串sub出现的次数;无指定范围则表示搜索整个字符串;
s = "hello world"
#find
print(s.find("e"), s.find("o"), s.find("g"), s.find("o", 4, 8), s.find("o", 8, 10))
#rfind
print(s.rfind("e"), s.rfind("o"), s.rfind("g"), s.rfind("o", 4, 8), s.rfind("o", 8, 10))

(三)Python学习之字符串常用操作(上)

s = "hello world"
#index
print(s.index("o"))
print(s.index("o", 6, 8))

(三)Python学习之字符串常用操作(上)

s = "hello world"
#index
print(s.index("g"))

(三)Python学习之字符串常用操作(上)

s = "hello world"
#count
print(s.count("o"))
print(s.count("o", 3, 5))

(三)Python学习之字符串常用操作(上)
四、字符串替换

  1. replace(‘old’, ‘new’ , count = None): 从左至右依次替换count次的old为new;当count为空时,整个字符串中的old均替换为new;
s = "elelelelelelelele"
print(s.replace("e", "*"))
print(s.replace("e", "*", 3))

(三)Python学习之字符串常用操作(上)
五、字符串去空格及去指定字符

  1. strip(chars = None): 去除两边字符chars;当chars为空时,去除两边空格;
  2. lstrip(chars = None): 去除左边字符chars;当chars为空时,去除左边空格;
  3. rstrip(chars = None): 去除右边字符chars;当chars为空时,去除右边空格;
  4. split(sep = None, maxsplit = None): 按从左至右的前maxspliit个指定字符sep分割字符串为数组;当sep为空时,按空格分隔;当maxsplit为空时,按全部的字符sep分割;(字符sep不包含在结果之中)
  5. rsplit(sep = None, maxsplit = None): 按从右至左的前maxspliit个指定字符sep分割字符串为数组;当sep为空时,按空格分隔;当maxsplit为空时,按全部的字符sep分割;(字符sep不包含在结果之中)
s = "   h e-l lo   "
t = "***h e-l lo***"
#strip,lstrip,rstrip,
print(s)
print(s.strip())
print(s.lstrip())
print(s.rstrip())
print(t)
print(t.strip("*"))
print(t.lstrip("*"))
print(t.rstrip("*"))

(三)Python学习之字符串常用操作(上)

s = "abc-de fg-hi jkl-mno-pq"
#split,rsplit
print(s)
print(s.split())
print(s.split("-"))
print(s.split("-", 2))
print(s.rsplit())
print(s.rsplit("-"))
print(s.rsplit("-", 2))

(三)Python学习之字符串常用操作(上)
六、字符串判断

  1. endswith(suffix, start=Nome, end=None): 在给定范围内判断字符串是否以suffix结尾;当给定范围为空时,判断整个字符串;
  2. startswith(suffix, start=Nome, end=None): 在给定范围内判断字符串是否以suffix开头;当给定范围为空时,判断整个字符串;
  3. isalnum(): 判断字符串中是否只包含字母和数字;
  4. isalpha(): 判断字符串中是否只包含字母和汉字;
  5. isdecimol(): 判断字符串是否为数字;
  6. isdigit(): 判断字符串是否为数字,较isdecimol()范围更大;
  7. isnumeric(): 判断字符串是否为数字,较isdigit()范围更大;
  8. isprintable(): 判断字符串是否不存在不可显示的字符,如\t \n;
  9. isspace(): 判断字符串是否全部都是空格;
  10. istitle(): 判断字符串是否是标题;
  11. isidentifier(): 判断字符串是否是标识符;
  12. isupper(): 判断字符串是否是全部大写;
  13. islower(): 判断字符串是否是全部小写;
test = "ljwan"
#startswith,endswith
print(test.startswith("lj"))
print(test.startswith("lj", 3, 7))
print(test.endswith("an"))
print(test.endswith("an", 1, 2))

(三)Python学习之字符串常用操作(上)

test1 = "ljwan520"
test2 = "ljwan520**"
test3 = "lj哈哈哈"
test4 = "lj哈哈哈520"
#isalnum,isalpha
print(test1.isalnum())
print(test2.isalnum())
print(test3.isalpha())
print(test4.isalpha())

(三)Python学习之字符串常用操作(上)

#isdecimol,isdigit,isnumeric
num1 = "1"
num2 = "①"
num3 = "四"
print(num1.isdigit(), num1.isdecimal(), num1.isnumeric())
print(num2.isdigit(), num2.isdecimal(), num2.isnumeric())
print(num3.isdigit(), num3.isdecimal(), num3.isnumeric())

(三)Python学习之字符串常用操作(上)

test1 = "lj520"
test2 = " lj520"
test3 = "lj\n520"
test4 = "lj\t520"
test5 = "     "
#isprintable,isspace
print(test1.isprintable())
print(test2.isprintable())
print(test3.isprintable())
print(test2.isspace())
print(test5.isspace())

(三)Python学习之字符串常用操作(上)

#istitle,isidentifier
test1 = "Lu Jing Wo Ai Ni"
test2 = "Lu jing wo ai ni"
test3 = "LU JING WO AI NI"
test4 = "lj_520"
print(test1.istitle())
print(test2.istitle())
print(test3.istitle())
print(test1.isidentifier())
print(test4.isidentifier())

(三)Python学习之字符串常用操作(上)

#isupper,islower
test1 = "Lu Jing Wo Ai Ni"
test2 = "lu jing wo ai ni"
test3 = "LU JING WO AI NI"
print(test1.isupper())
print(test2.isupper())
print(test3.isupper())
print(test1.islower())
print(test2.islower())
print(test3.islower())

(三)Python学习之字符串常用操作(上)

分类:

技术点:

相关文章: