python内置函数判断字符类型_python数据类型之字符串部分内置函数

字符串的格式化>>> CHA = 123

>>> a = 'test'

>>> print('My name is %s' %a)

My nameistest>>> print('I am %d years old'%CHA)

I am123years old

format_string%(string_to_convert1,string_to_convert2)>>> 'My name is %s and I am %d years old' %(a,CHA)'My name is test and I am 123 years old'

%s 将值转化为str()函数表达的字符串;%d 将值转化为十进制数;%c 将值转化为单个字符;%r 将值转化为repr()函数表达的字符串;

字符串内置函数:

capitalize() 将字符串中第一个字符转换为大写

>>> a = 'hahah'

>>> a.capitalize()

'Hahah'

find('str') 检查字符串中是否包含str,找不到返回-1;

>>> a = 'My name is haha'

>>> a.find('name')

3

>>> a.find('my')

-1

index() 如果找不到返回异常

>>> a = 'My name is haha'

>>> a.index('name')

3

>>> a.index('my')

Traceback (most recent call last):

File "", line 1, in

ValueError: substring not found

lower() 转换所有大写字符转换成小写

>>> a = 'MY NAME IS HAHA'

>>> a.lower()

'my name is haha'

title() 把字符串标题化,所有字符开头是大写的;

>>> a = 'my name is haha'

>>> a.title()

'My Name Is Haha'

istitle() 判断是否是标题化的字符串;

>>> a = a.title()

>>> a

'My Name Is Haha'

>>> a.istitle()

True

lstrip() 截掉字符串左边的空格;

>>> a = ' my name is haha'

>>> a

' my name is haha'

>>> a.lstrip()

'my name is haha'

replace('old','new') 替换操作;

>>> a = a.lstrip()

>>> a

'my name is haha'

>>> a.replace('haha','hello')

'my name is hello'

rstrip() 截掉字符串右边的空格;

>>> a = 'my name is hah '

>>> a

'my name is hah '

>>> a.rstrip()

'my name is hah'

split() 以str做分割字符串;

>>> a = 'zhangsan lisi wangye'

>>> a.split()

['zhangsan', 'lisi', 'wangye']

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值