2.字符串魔法

 字符串魔法

字符串

  字符串由单引号或者双引号引起所组成。如:‘123’、‘师范大学’、‘i love you !’……

字符串魔法

  1.join(self, iterable: Iterable[str]) -> str: ... :将字符串中的每一个元素按照指定分隔符进行拼接。

 1 test = '你是风儿我是沙'
 2 print(test)
 3 # =>你是风儿我是沙
 4 t = ' ' # 用空格进行拼接
 5 v = t.join(test)
 6 print(v)
 7 # =>你 是 风 儿 我 是 沙
 8 t = '*'  # 用*进行拼接
 9 v = t.join(test)
10 print(v)
11 # =>你*是*风*儿*我*是*沙
View Code

  

  2.capitalize(self) -> str:... :单个单词时首字母大写,有多个单词时只将第一个字母大写。

1 test = 'alex'
2 v = test.capitalize()
3 print(v)
4 # =>Alex
5 test = 'alex i love you'
6 v = test.capitalize()
7 print(v)
8 # =>Alex i love you
View Code

  

  3.casefold(self) -> str: .../---lower(self) -> str: ...:所有字母变小写;casefold更高级,很多未知的符号都对相应变小写。

1 test = 'I lOVe You'
2 v1 = test.casefold()
3 print(v1)
4 # =>i love you
5 v2 = test.lower()
6 print(v2)
7 # =>i love you
View Code

  

  4.center(width,'指定字符'):设置宽度,并将内容居中 ;width代指总宽度 ,空白未知填充,一个字符,可有可无。

  5.ljust(width,'指定字符') :设置宽度,将内容居左。

  6.rjust(width,'指定字符'):设置宽度,将内容居右。

 1 test = 'Love'
 2 v1 = test.center(10)
 3 v2 = test.ljust(10)
 4 v3 =  test.rjust(10)
 5 print(v1)
 6 # =>   Love
 7 print(v2)
 8 # =>Love
 9 print(v3)
10 # =>      Love
11 v4 = test.center(20,'@')
12 v5 = test.ljust(20,'@')
13 v6 = test.rjust(20,'@')
14 print(v4)
15 # =>@@@@@@@@Love@@@@@@@@
16 print(v5)
17 # =>Love@@@@@@@@@@@@@@@@
18 print(v6)
19 # =>@@@@@@@@@@@@@@@@Love
View Code

  

  7.count(self, x: Text, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ...  :count(寻找对象(必填),开始位置,结束位置),在字符串中寻找,寻找子序列的出现次数,可设置开始与结束的位置。

1 test = 'alexalexdsdg'
2 v1 = test.count('l')
3 print(v1)
4 # =>2
5 v2 = test.count('a',3,8)
6 print(v2)
7 # =>1
View Code

  

  8.endswith(self, suffix: Union[Text, Tuple[Text, ...]], start: Optional[int] = ..., end: Optional[int] = ...) -> bool: ...:判断字符串是否以……结尾,是则为True,否则为False。

  9.startswith(self, suffix: Union[Text, Tuple[Text, ...]], start: Optional[int] = ..., end: Optional[int] = ...) -> bool: ...:判断字符串是否以……开始,是则为True,否则为False。

 1 test = 'i love you alex'
 2 v1 = test.endswith('a')
 3 v2 = test.endswith('lex')
 4 v3 = test.startswith('i')
 5 v4 = test.startswith('i l')
 6 print(v1)
 7 print(v2)
 8 print(v3)
 9 print(v4)
10 # => False
11 # => True
12 # => True
13 # => True
View Code

  

  10.find(self, sub: Text, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ...:find(寻找对象,开始区间,闭合区间),从开始往后找,找到第一个之后,获取其位置,未找到返回-1。

1 test = 'agjkdgjk'
2 v = test.find('jk')
3 print(v)
4 # =>2
5 v1 = test.find('c',3,6)
6 print(v1)
7 # =>-1
View Code

  

  11.format(self, *args: Any, **kwargs: Any) -> str: ...:格式化,将一个字符串中的占位符替换为指定的值。

1 test = 'i an {name},age {a}'
2 print(test)
3 # =>i an {name},age {a}
4 v = test.format(name='alex',a=24)
5 print(v)
6 # =>i an {name},age {a}
View Code

  

  12.format_map({key1:value1,key2:value2}) :格式化,传入的值为字典 eg:{"name":'alex',"a":24}。

1 test = 'i an {name},age {a}'
2 v1=test.format(name='alex',a=24)
3 print(v1)
4 # =>i an alex,age 24
5 v2=test.format_map({"name":'alex',"a":24})
6 print(v2)
7 # =>i an alex,age 24
View Code

  

  13.index(self, sub: Text, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ...  : 寻找对象,寻找指定字符在字符串中的位置,找不到会报错,建议使用find()方法。

1 test = 'alexalexdsdg'
2 v = test.index('g')
3 print(v)
4 # => 11
View Code

   

  14.isalnum(self) -> bool: ... :可用以判断字符串中是否只包含字母和数字。

1 test = 'alexalexdsdg455'
2 v = test.isalnum()
3 print(v)
4 # => True
5 test1 = 'alexalexdsdg455_?'
6 v2 = test1.isalnum()
7 print(v2)
8 # => False
View Code

  

  15.isalpha(self) -> bool: ...  :可用以判断字符串中是否只包含字母和汉字。

1 test = 'fjsldjgjlj784'
2 v = test.isalpha()
3 print(v)
4 # => False
5 test1 = 'jlkjg你好'
6 v1 = test1.isalpha()
7 print(v1)
8 # => True
View Code

  

  16.isdecimal(self) -> bool: ...   :可用以判断字符串中是否只包含阿拉伯数字(1,2,3,……)。

  17.isdigit(self) -> bool: ...    :可用以判断字符串中是否包含数字,特殊数字也可以进行判断。

  18.isnumeric(self) -> bool: ...    :可用以判断字符串中是否只包含数字,既可对特殊数字判断,也可对中文数字判断。

 1 test = '23435454'
 2 v1 = test.isdecimal()
 3 v2 = test.isdigit()
 4 v3 = test.isnumeric()
 5 print(v1,v2,v3)
 6 # => True True True
 7 test1 = ''
 8 v4 = test1.isdecimal()
 9 v5 = test1.isdigit()
10 v6 = test.isnumeric()
11 print(v4,v5,v6)
12 # => False True True
13 test2 = ''
14 v7 = test2.isdecimal()
15 v8 = test2.isdigit()
16 v9 = test2.isnumeric()
17 print(v7,v8,v9)
18 # => False False True
View Code

  

  19.expandtabs(self, tabsize: int = ...) -> str: ...   :expandtabs(数字):以数字为个数对字符串进行断句,遇到制表符(\t)补空格,可用以制表格。

 1 test='12345678\t9'
 2 v = test.expandtabs(6) #以6个字符为数,遇到制表符补空格
 3 print(v)
 4 # => 12345678    9 #78+4个空格
 5  test='username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\n'
 6 v = test.expandtabs(20)
 7 print(v)  
 8 # => username            email               password
 9      laiying             ying@q.com          123
10      laiying             ying@q.com          123
11      laiying             ying@q.com          123
View Code

  

  20.isidentifier(self) -> bool: ...    :字母、数字、下划线、标识符:def class 判断字符串是否符合规范的变量名称。

 1 test = 'class'
 2 v = test.isidentifier()
 3 print(v)
 4 # => True
 5 test1 = '124dghhg'
 6 v1 = test1.isidentifier()
 7 print(v1)
 8 # => False
 9 test2 = '_124dghhg'
10 v2 = test2.isidentifier()
11 print(v2)
12 # => True
View Code

  

  21.islower(self) -> bool: ...    :判断字符串是否都为小写。

  22.isupper(self) -> bool: ...    :判断字符串是否都为大写。

  23.lower(self) -> str: ...       :将字符串全部转换为小写。

  24.upper(self) -> str: ...      :将字符串全部转换为大写。

1 test = 'i LoVE yOu'
2 v1 = test.islower() #判断是否都为小写
3 v2 = test.lower() #全部转换为小写
4 v3 = test.isupper() #判断是否都为大写
5 v4 = test.upper() #全部转换为大写
6 print(v1,v2)
7 # => False i love you
8 print(v3,v4)
9 # => False I LOVE YOU
View Code

  

  25.isprintable(self) -> bool: ...    :可用以判断字符串中是否含有不可显示的字符或符号(不可显示:打印出来不显示,如:'oic\tdgn'中的\t和\n即不可见)。

1 test = 'alexAxdsdg你好——\t'
2 v = test.isprintable()
3 print(v)
4 # => False
5 test1 = '78678ghkdhg'
6 v1 = test1.isprintable()
7 print(v1)
8 # => True
View Code

  

  26.isspace(self) -> bool: ...   :判断字符串是否全部都为空格。

1 test = '  fsdg'
2 v = test.isspace()
3 print(v)
4 # => False
5 test1 = '    '
6 v1 = test1.isspace()
7 print(v1)
8 # => True
View Code

  

  27.istitle(self) -> bool: ...    :判断字符串是否为标题,即所有单词首字母大写。

1 test = 'I Love You'
2 v = test.istitle()
3 print(v)
4 # => True
5 test1 = 'I love you'
6 v1 = test1.istitle()
7 print(v1)
8 # => False
View Code

  

  28.strip(): 默认去除字符串左右两端空格,\t、\n、指定字符也可去除。

  29.lstrip(): 默认去除字符串左端空格,\t、\n、指定字符也可去除。

  30.rstrip(): 默认去除字符串右端空格,\t、\n可去除。

 1 test = ' love '
 2 v = test.strip() #去两端空格
 3 v = test.lstrip() #去左边空格
 4 v = test.rstrip() #去右边空格
 5 print(v)
 6 test = 'iloveyoud '
 7 v = test.lstrip('idlov') #去除指定字符,子序列匹配,含有几个去几个,最多匹配
 8 print(v)
 9 # => eyoud
10 v = test.rstrip('d')
11 print(v)
12 # => iloveyoud #从右边无法去除指定字符
View Code

  

  31.maketrans(): 进行替换对应。

  32.translate():  进行替换。

1 test1 = 'abcdefg'
2 test2 = '1234567'
3 v = 'i love apple i can  fly i eat grass'
4 m = str.maketrans(test1,test2) #进行替换对应
5 new_v = v.translate(m) #进行替换
6 print(new_v)
7 # => i lov5 1ppl5 i 31n  6ly i 51t 7r5ss
View Code

  

  33.partition():  根据指定字符对字符串进行分割,保留指定字符,从左边开始,只分三份。

  34.rpartition():  根据指定字符对字符串进行分割,保留指定字符,从右边开始,只分三份。

  35.split('对象','个数'):   根据指定字符对字符串进行分割,不保留指定字符,从左边开始,可设置分割个数。

  36.rsplit('对象','个数'):   根据指定字符对字符串进行分割,不保留指定字符,从右边开始,可设置分割个数。

 1 test = 'klagjkjgsdajkjgcadajiiutu'
 2 v = test.partition('a') #根据单个‘a’对字符串进行分割,从左边只找第一个,只分三份
 3 print(v)
 4 # => ('kl', 'a', 'gjkjgsdajkjgcadajiiutu')
 5 v = test.rpartition('a') #根据单个‘a’对字符串进行分割,从右边只找第一个,只分三份
 6 print(v)
 7 # => ('klagjkjgsdajkjgcad', 'a', 'jiiutu')
 8 v = test.split('a') #根据全部‘a’对字符串进行分割,分多段
 9 print(v)
10 # => ['kl', 'gjkjgsd', 'jkjgc', 'd', 'jiiutu']
11 v = test.rsplit('a') #根据全部‘a’对字符串进行分割,分多段
12 print(v)
13 # =>['kl', 'gjkjgsd', 'jkjgc', 'd', 'jiiutu']
View Code

  

  37.splitlines(self, keepends: bool = ...) -> List[str]: ...    :对字符串进行分割,只能根据\n进行分割,True,False 表示是否保留换行符\n。

1 test = 'dsafsdgdg\ngfdhghgh\ngdgfdhgh'
2 v = test.splitlines(False)
3 print(v)
4 # => ['dsafsdgdg', 'gfdhghgh', 'gdgfdhgh']
View Code

  

  38.swapcase(self) -> str: ...  :大小写转换。

1 test = 'fsDG'
2 v = test.swapcase()
3 print(v)
4 # => FSdg
View Code

  

  39.len():  :判断字符串长度,也可判断列表长度。

1 test = '你真棒!'
2 list = [11,22,33,44,55]
3 v = len(test)
4 print(v)
5 # => 4
6 print(len(list))
7 # => 5
View Code

  

  40.zfill(self, width: int) -> str: ...   :居左只填充0,可设置宽度width。

1 test = 'love'
2 v = test.zfill(20)
3 print(v)
4 # => 0000000000000000love
View Code

  

  41.replace(self, old: AnyStr, new: AnyStr, count: int = ...) -> AnyStr: ..  :替换,旧字符串替换新字符串,可设置替换个数。

1 test = 'love you love you'
2 v =test.replace('o','m')
3 print(v)
4 # => lmve ymu lmve ymu
5 test = 'love you love you'
6 v =test.replace('o','m',2) #可设置参数,进行替换个数的选择
7 print(v)
8 # => lmve ymu love you
View Code

 

posted @ 2020-04-27 16:45  星落……  阅读(135)  评论(0)    收藏  举报