当前位置: 移动技术网 > IT编程>脚本编程>Python > Python自学笔记之字符串的操作

Python自学笔记之字符串的操作

2019年08月04日  | 移动技术网IT编程  | 我要评论

摄政王的金牌鬼妃,nba虚报身高蔚然成风,逆杀神魔

1.将字符串全部变为小写:lower()

           casefold() 范围更广

2.将字符串全部变为大写:upper()

3.判断是否大小写:isupper()

        islower()

 4.居中:center(width,fillchar=none)

>> 'python'.center(10,'-')
>> '--python--'

5.字符串中寻找子序列出现次数:count(char,start=none,end=none)

6.判断字符串是否以xx开头或结尾:startswith(char,start,end)

                  endswith(char,start,end)

7.从开始往后找,找到第一个之后获取其位置:find()

>> 'pythonpython'.find('py')
>> 1

 8.第一个字符或字符串出现的位置:index()

>> 'pythonpython'.index('y')
>> 1

9.判断字符串中是否只包含字母和数字:isalnum()

10.判断字符串中是否只包含符号:isalpha()

11.判断当前输入是否是数字:isdecimal()

            isdigit()范围更广

            isnumeric()范围较上更广

>> '②'.isdecimal()
>> false
>> '②'.isdigit()
>> true
>> '二'.isdigit()
>> false
>> '二'.isnumeric()
>> true

 12.大小写转换(大写变小写小写变大写):swapcase()

>> 'python'.swapcase()
>> 'python'

13.判断字符串是否是有效的 python 标识符,可用来判断变量名是否合法:isidentifier()

14.是否存在不可显示的字符:isprintable() 输出false表示含有不可打印的信息\t

>> 'python\tpython'.isprintable()
>>  false

15.字符串是否全部是空格:isspace()

16.字符串转换为标题格式:title()

>> 'python is good'.title()
>> 'python is good'

17.判断字符串是否为标题:istitle()

18.将字符串每个字符之间插入制定字符或字符串:join()

>> ' '.join('新宝岛')
>> '新 宝 岛'

判断字符串是否为标题:istitle()

19.左对齐填充:ljust(width,fillchar=none)

20.右对齐填充:rjust(width,fillchar=none)

>> 'python'.ljust(20,'_')
>> 'python______________'

21.去除匹配的字符strip()

        lstrip()

        rstrip()

>> 'pythonohtyp'.strip('9py')
>> 'thonoht'

22.字符串的分割:partition()

>> 'pypypypypypy'.partition('y')
>> ('p', 'y', 'pypypypypy')

 

        rpartition()

>> 'pypypypypypy'.rpartition('y')
>> ('pypypypypyp', 'y', '')

       split(char,个数)

>> 'python'.split('h')
>> ['pyt', 'on']

 23.用某个字符串代替原有的某段字符串:replace(oldchar,newchar,替换次数)

>> 'python'.replace('py','py')
>> 'python'

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网