python基础-字符串方法


字符串方法

示例:有许多字符串方法可以让我们格式化字符串。请参阅以下示例中的一些字符串方法:

1.capitalize()

将字符串的第一个字符转换为大写字母

代码如下(示例):

challenge = sys_user
print(challenge.capitalize())  # Sys_user

2.count()

统计元素出现数量
代码如下(示例):

challenge = 'thirty days of python'
print(challenge.count('y')) # 3
print(challenge.count('y', 7, 14)) # 1, 
print(challenge.count('th')) # 2`

该处使用的url网络请求的数据。

3.endswith()

检查元素是否在末尾

代码如下(示例):

challenge = 'thirty days of python'
print(challenge.endswith('on'))   # True
print(challenge.endswith('tion')) # False

4.expandtabs()

用空格替换制表符,默认制表符大小为 8。它需要制表符大小参数

challenge = 'thirty\tdays\tof\tpython'
print(challenge.expandtabs())   # 'thirty  days    of      python'
print(challenge.expandtabs(10)) # 'thirty    days      of        python'

5.find()

返回子字符串第一次出现的索引,如果没有找到返回-1
代码如下(示例):

challenge = 'thirty days of python'
print(challenge.find('y'))  # 5
print(challenge.find('th1')) # -1

6.rfind()

返回子串最后一次出现的索引,如果没有找到返回-1
代码如下(示例):

challenge = 'thirty days of python'
print(challenge.rfind('y'))  # 16
print(challenge.rfind('th')) # 17
print(challenge.rfind('th1111')) #-1

7.format()

格式化代码 和 %s %d 是一样的 参考python基础-字符串

first_name = 'Asabeneh'
last_name = 'Yetayeh'
age = 250
job = 'teacher'
country = 'Finland'
sentence = 'I am {} {}. I am a {}. I am {} years old. I live in {}.'.format(first_name, last_name, age, job, country)
print(sentence) # I am Asabeneh Yetayeh. I am 250 years old. I am a teacher. I live in Finland.

radius = 10
pi = 3.14
area = pi * radius ** 2
result = 'The area of a circle with radius {} is {}'.format(str(radius), str(area))
print(result) # The area of a circle with radius 10 is 314

8.index()

检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。 (默认 0 和字符串长度 - 1)

challenge = 'thirty days of python'
sub_string = 'da'
print(challenge.index(sub_string))  # 7
print(challenge.index(sub_string, 9)) # error

9.rindex()

返回子字符串的最高索引,附加参数指示开始和结束索引(默认 0 和字符串长度 - 1)

challenge = 'thirty days of python'
sub_string = 'da'
print(challenge.rindex(sub_string))  # 8
print(challenge.rindex(sub_string, 9)) # error
print(challenge.rindex(sub_string, 7)) # 7

10.isalnum()

检测字符串是否由字母和数字组合
如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False

challenge = 'ThirtyDaysPython'
print(challenge.isalnum()) # True

challenge = '30DaysPython'
print(challenge.isalnum()) # True

challenge = 'thirty days of python'
print(challenge.isalnum()) # False, space is not an alphanumeric character

challenge = 'thirty days of python 2019'
print(challenge.isalnum()) # False

11.join()

拼接字符串

web_tech = ['HTML', 'CSS', 'JavaScript', 'React']
result = ' '.join(web_tech)
print(result) # 'HTML CSS JavaScript React'
web_tech = ['HTML', 'CSS', 'JavaScript', 'React']
result = '# '.join(web_tech)
print(result) # 'HTML# CSS# JavaScript# React'

12.strip()

删除字符串

challenge = 'thirty days of pythoonnn'
print(challenge.strip('noth')) # 'irty days of py'

13.replace()

替换字符串

challenge = 'thirty days of python'
print(challenge.replace('python', 'coding')) # 'thirty days of coding'

14.split()

分割字符串

challenge = 'thirty days of python'
print(challenge.split()) # ['thirty', 'days', 'of', 'python']
challenge = 'thirty, days, of, python'
print(challenge.split(', ')) # ['thirty', 'days', 'of', 'python']

15.startswith()

检查字符串是否以指定的字符串开头

challenge = 'thirty days of python'
print(challenge.startswith('thirty')) # True

challenge = '30 days of python'
print(challenge.startswith('thirty')) # False

总结

提示:这里对文章进行总结:
喜欢-就点赞

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值