python中str函数举例_python初学day2--(字符串(str)内置函数用法汇总,附带例子)...

Str内部方法用法整理

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

s = 'hello'

result = s.capitalize()

print(result)              结果: Hello

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

s = 'HELLO'

result = s.casefold()

print(result)      结果:hello

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

s = 'HELLO'

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

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

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

s = 'HELLO'

result = s.count('L')

print(result)      结果: 2

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

s = 'HELLO'

result = s.endswith('LO')

result1 = s.endswith('LX')

print(result)    结果:True

print(result1)   结果:False

6, def  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,def  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, def 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

result1 = s.index('LX')

ValueError: substring not found

9, def 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,def  isalnum(self):判断一个字符串是否仅是由数字和字母组成

s = 'flowers12'

s1 = 'flowers12!!'

result = s.isalnum()

result1 = s1.isalnum()

print(result)    结果: True

print(result1)   结果: False

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

s = 'flowers'

s1 = 'flowers12'

result = s.isalpha()

result1 = s1.isalpha()

print(result)     结果: True

print(result1)    结果: False

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

s = '1223'

s1 = 'flowers12'

result = s.isdigit()

result1 = s1.isdigit()

print(result)     结果: True

print(result1)    结果: False

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

s = '1223'

s1 = '0x1223'

result = s.isdecimal()

result1 = s1.isdecimal()

print(result)     结果: True

print(result1)    结果: False

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

s = '12hello'

s1 = 'hello'

result = s.isidentifier()

result1 = s1.isidentifier()

print(result)     结果: False

print(result1)    结果: True

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

s = 'Hello'

s1 = 'hello'

result = s.islower()

result1 = s1.islower()

print(result)     结果: False

print(result1)    结果: True

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

s = '1256'

s1 = 'test1256'

result = s.isnumeric()

result1 = s1.isnumeric()

print(result)     结果: True

print(result1)    结果: False

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

s  = '  '

s1 = 'test  1256'

result = s.isspace()

result1 = s1.isspace()

print(result)     结果: True

print(result1)    结果: False

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

s = 'Hello World'

s1 = 'hello world'

result = s.istitle()

result1 = s1.istitle()

print(result)     结果: True

print(result1)    结果: False

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

s = 'HELLO'

s1 = 'Hello'

result = s.isupper()

result1 = s1.isupper()

print(result)     结果: True

print(result1)    结果: False

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

s = 'HELLO'

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

print(result)     结果: xHELLOxHELLOxHELLO*HELLO*

21,def   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

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

s = 'HELLOworld'

result = s.lower()

result1 = s.upper()

print(result)     结果: helloworld

print(result1)    结果: HELLOWORLD

23,def  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

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

s = 'HELLOworld'

result = s.partition('wo')

result1 = s.partition('ss')

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

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

25, def  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

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

s = 'helloworddoor'

result = s.split('o')

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

27,def  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', '']

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

s = 'helloworld'

result = s.startswith('he')

result1 = s.startswith('el')

print(result)        结果: True

print(result1)      结果: False

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

s = 'HELLOworld'

result = s.swapcase()

print(result)     结果: helloWORLD

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

s = 'this is a buautiful flowers'

result = s.title()

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值