设为首页 加入收藏

TOP

Python自学笔记之字符串的操作
2019-08-15 00:09:54 】 浏览:52
Tags:Python 自学 笔记 字符串 操作

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'

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇面向对象:类空间问题以及类之间的.. 下一篇python3 简陋的学生信息管理系统

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目