python初学day2--(字符串(str)内部功能简介)

Posted python-exercise

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python初学day2--(字符串(str)内部功能简介)相关的知识,希望对你有一定的参考价值。

                                 Str内部功能简介

1,pitalize(self): 将字符串首字母变成大写

         s = ‘hello‘

         result = s.capitalize()

         print(result)              结果: Hello

2,casefold(self): 见字符串变成小写   

         s = ‘HELLO‘

        result = s.casefold()

        print(result)      结果:hello

3,center(self, width, fillchar=None):经字符串居中,默认用空格填充    

        s = ‘HELLO‘

        result = s.center(20,‘*‘)

        print(result)     结果: *******HELLO********

4, count(self, sub, start=None, end=None): 统计一个字符在字符串中出现的次数,并且可以指定起,末位置。    

        s = ‘HELLO‘

        result = s.count(‘L‘)

        print(result)      结果: 2

5, endswith(self, suffix, start=None, end=None):判断一个字符串是否已某个字符或者字符串结尾, 并且可以指定起,末位置。  

        s = ‘HELLO‘

        result = s.endswith(‘LO‘)

        result1 = s.endswith(‘LX‘)

        print(result)    结果:True

        print(result1)   结果:False

6, expandtabs(self, tabsize=8):将字符串中的tab键用空格代替,默认8个空格

        s = ‘HELLO\tWORLD!‘

        result = s.expandtabs()

        result1 = s.expandtabs(20)

        print(result)     结果: HELLO   WORLD!

        print(result1)    结果: HELLO               WORLD!

7, find(self, sub, start=None, end=None): 在一个字符串中查找一个字符或者字符串,如果找到返回下标,如果找不到返回-1

        s = ‘HELLOWORLDHELLO!‘

        result = s.find(‘LO‘)

        result1 = s.find(‘LX‘) print(result)    结果:3

        print(result1)   结果:-1

8, index(self, sub, start=None, end=None): 在一个字符串中查找一个字符或者字符串,如果找到返回下标,如果找不到抛异常(报错)

        s = ‘HELLOWORLDHELLO!‘

       result = s.index(‘LO‘)

       print(result)     结果:3

       s = ‘HELLOWORLDHELLO!‘

       result1 = s.index(‘LX‘)

       print(result1)    如下结果:  报错内容

                               Traceback (most recent call last):       File "C:/python/test2.py", line 3, in <module>      

                                                                                           result1 = s.index(‘LX‘)

                                                                                           ValueError: substring not found

9, format(self, *args, **kwargs):字符串格式化

        s = ‘flowers‘

        print(‘This is {} !‘.format(s))

        print(‘This is {0} flowers, That is {1} flowers‘.format(‘red‘,‘yellow‘))

        print(‘Two colour {name} and {name1}‘.format(name1 = ‘yellow‘, name = ‘red‘))      

       结果:           

                This is flowers !

                This is red flowers, That is yellow flowers

                Two colour red and yellow

10, isalnum(self):判断一个字符串是否仅是由数字和字母组成

           s = ‘flowers12‘

           s1 = ‘flowers12!!‘

           result = s.isalnum()

           result1 = s1.isalnum()

           print(result)    结果: True

           print(result1)   结果: False

11, isalpha(self): 判断一个字符串是否仅由字母组成

         s = ‘flowers‘

         s1 = ‘flowers12‘

         result = s.isalpha()

         result1 = s1.isalpha()

         print(result)     结果: True

         print(result1)    结果: False

12, isdigit(self):判断一个字符串是否是数字:     

         s = ‘1223‘

         s1 = ‘flowers12‘

         result = s.isdigit()

         result1 = s1.isdigit()

         print(result)     结果: True

         print(result1)    结果: False

13, isdecimal(self):判断一个字符串是否是十进制数     

         s = ‘1223‘

         s1 = ‘0x1223‘

         result = s.isdecimal()

         result1 = s1.isdecimal()

         print(result)     结果: True

         print(result1)    结果: False

14, isidentifier(self):判断一个标识符是否合法

         s = ‘12hello‘

         s1 = ‘hello‘

         result = s.isidentifier()

         result1 = s1.isidentifier()

         print(result)     结果: False

         print(result1)    结果: True

15, islower(self): 判断一个字符串是否是小写。

         s = ‘Hello‘

         s1 = ‘hello‘

         result = s.islower()

         result1 = s1.islower()

         print(result)     结果: False

         print(result1)    结果: True

16, isidentifier(self):判断一个字符串是否只有数字

          s = ‘1256‘

          s1 = ‘test1256‘

          result = s.isnumeric()

          result1 = s1.isnumeric()

          print(result)     结果: True

          print(result1)    结果: False

17,isspace(self): 判断字符串是否是空格

          s  = ‘  ‘

          s1 = ‘test  1256‘

          result = s.isspace()

          result1 = s1.isspace()

          print(result)     结果: True

          print(result1)    结果: False

18,istitle(self):判断一个字符串是否为标题

        s = ‘Hello World‘

        s1 = ‘hello world‘

        result = s.istitle()

        result1 = s1.istitle()

        print(result)     结果: True

        print(result1)    结果: False

19,isupper(self):判断字符串是否为大写

        s = ‘HELLO‘

        s1 = ‘Hello‘

        result = s.isupper()

        result1 = s1.isupper()

        print(result)     结果: True

        print(result1)    结果: False

20,join(self, iterable):用字符串将参数(interable)分割

       s = ‘HELLO‘

       result = s.join(‘xxx**‘)

       print(result)     结果: xHELLOxHELLOxHELLO*HELLO*

21, ljust(self, width, fillchar=None) and rjust(self, width, fillchar=None)      从左或者从右用指定字符填充指定宽度

       s = ‘HELLO‘

       result = s.ljust(12,‘*‘)

       result1 = s.rjust(12,‘*‘)

       print(result)     结果: HELLO*******

       print(result1)    结果: *******HELLO

21, lower(self) and upper(self):将字符串变成小写 and 变成大写

       s = ‘HELLOworld‘

       result = s.lower()

       result1 = s.upper()

       print(result)     结果: helloworld

       print(result1)    结果: HELLOWORLD

22, lstrip(self, chars=None) and  rstrip(self, chars=None):从左边或者右边移除指定的字符

         s = ‘HELLOworld‘

         result = s.lstrip(‘HE‘)

         result1 = s.rstrip(‘ld‘)

         print(result )      结果: LLOworld

         print(result1)     结果: HELLOwor

23,partition(self, sep): 将一个字符串按照指定的字符分割成一个元组     

          s = ‘HELLOworld‘

          result = s.partition(‘wo‘)

          result1 = s.partition(‘ss‘)

          print(result)     结果: (‘HELLO‘, ‘wo‘, ‘rld‘)

          print(result1)    结果: (‘HELLOworld‘, ‘‘, ‘‘)

24, replace(self, old, new, count=None):用新的字符替代老的字符,count指定替代个数,默认全部替代

          s = ‘hello word door‘

          result = s.replace(‘o‘,‘T‘)

          result1 = s.replace(‘o‘,‘T‘,2)

          print(result)      结果: hellT wTrd dTTr

          print(result1)     结果: hellT wTrd door

25,split(self, sep=None, maxsplit=-1): 将一个字符串用指点的字符分割成一个列表,指定的字符自动去除。

        s = ‘helloworddoor‘

        result = s.split(‘o‘)

        print(result)        结果: [‘hell‘, ‘w‘, ‘rdd‘, ‘‘, ‘r‘]

26,splitlines(self, keepends=None):,将一个字符串按照行分割成一个列表

          s = ‘‘‘

                 this is a cat

                  that is a dog

               ‘‘‘

          result = s.splitlines()

          print(result)     结果: [‘‘, ‘this is a cat‘, ‘that is a dog‘, ‘‘]

27,startswith(self, prefix, start=None, end=None): 判断一个字符串是否以指导的字符结尾。

         s = ‘helloworld‘

         result = s.startswith(‘he‘)

         result1 = s.startswith(‘el‘)

         print(result)        结果: True

         print(result1)      结果: False

28, swapcase(self):将一个字符串大小写转换     

          s = ‘HELLOworld‘

          result = s.swapcase()

          print(result)     结果: helloWORLD

29,title(self):将一个字符串转化为标题

         s = ‘this is a buautiful flowers‘

         result = s.title()

         print(result)     结果: This Is A Buautiful Flowers

30,maketrans(self, *args, **kwargs) and maketrans(self, *args, **kwargs):    用映射的形势替换对应字符,最后一个参数是去除该字符。

 

以上是关于python初学day2--(字符串(str)内部功能简介)的主要内容,如果未能解决你的问题,请参考以下文章

Python之路 day2 字符串/元组/列表/字典互转

ptyhon之路day2

day2—Python基本知识的补充:对象的方法

Python学习笔记-Day2-Python基础之字符串操作22222222222222222222222222222222

新手小白 python之路 Day2 (列表应用)

Coy的Python之路--Day2