python小白之路:第四章数据类型第一节详解字符串

capitalize()

返回原字符串的副本,将字符串的第一个字母变成大写,其他字母变小写

str = 'hello word'
print(str.capitalize())
# Hello word

center(width[,fillchar])

返回一个指定的宽度width居中的字符串,fillchar为填充的字符,默认为空格。

   str = 'hello word'
    print(str.center(15,'%'))
    # %%%hello word%%

ljust(width[,fillchar]) & rjust(width[,fillchar])

返回一个指定的宽度width居左的字符串,fillchar为填充的字符,默认为空格。

返回一个指定的宽度width居右的字符串,fillchar为填充的字符,默认为空格。

  str = 'hello word'
    print(str.ljust(15,'%'))
    print(str.rjust(15,'%'))
    # hello word%%%%%
    # %%%%%hello word

count(sub, start= 0,end=len(string))

用于统计字符串里某个字符出现的次数,可选参数为在字符串搜索的开始与结束位置。

 str = 'hello word'
    print(str.count('l'))
    # 2
    print(str.count('l',3))
    # 1

encode(encoding=‘UTF-8’,errors=‘strict’)

以指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。

str = 'hello word'
print(str.encode('UTF-8'))
# b'hello word'

find(sub, start=0, end=len(string))

检测字符串中是否包含子字符串 sub ,返回其最小索引位置。如果指定 start(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果不包含索引值,返回-1。

 str = 'hello word'
    print(str.find('l',2))
    # 2
    print(str.find('a'))
    # -1

rfind()

类似find(),返回子字符串 sub 在字符串内被找到的最大(最右)索引

str = 'hello word'
print(str.rfind('l',2))
# 3

index(sub, start=0, end=len(string))

重点记忆

检测字符串中是否包含子字符串 sub ,如果指定 start(开始) 和 end(结束) 范围,则检查是否包含在指定范围内。同find,区别是找不到会报错

str = 'hello word'
print(str.index('l',2))
# 2

rindex()

同理find rfind

isalnum()

检测字符串是否由中英文和数字组成。

 str = 'hello word'
    print(str.isalnum())
    # False
    str = '1hello在word'
    print(str.isalnum())
    # True

isalpha()

方法检测字符串是否只由字母或文字组成。

   str = 'helloword'
    print(str.isalpha())
    # True
    str = '你好世界'
    print(str.isalpha())
    # True

isdigit()

检测字符串是否只由数字组成。

str = '1hello在word'
print(str.isdigit())
# False
str = '1'
print(str.isdigit())
# True

lower() & islower()

将字符串里的所有大写英文字母转为小写

检测字符串是否由小写字母组成。

str = 'Hello Word'
print(str.lower())
print(str.islower())
# hello word
# False

isnumeric()

检测字符串是否只由数字组成,数字可以是: Unicode 数字,全角数字(双字节),罗马数字,汉字数字。指数类似 ² 与分数类似 ½ 也属于数字。

str = '1hello在word'
print(str.isnumeric())
# False
str = '一'
print(str.isnumeric())
# True

isspace()

检测字符串是否只由空白字符组成。

str = 'Hello Word'
print(str.isspace())
# False

upper() & isupper()

将字符串里的所有小写英文字母转为大写

检测字符串中所有的字母是否都为大写。

str = 'Hello Word'
print(str.upper())
print(str.isupper())
# HELLO WORD
# False

swapcase()

将字符串中的大写字母转为小写字母,小写字母转为大写字母

str = 'Hello Word'
print(str.swapcase())
# hELLO wORD

title()

返回副本字符串,把字符串中的每个单词首字母大写

str = 'hello word'
print(str.title())
# Hello Word

join(iterable)

重点记忆

将序列中的元素以指定的字符连接生成一个新的字符串。

str = 'hello word'
print('@'.join(str))
# h@e@l@l@o@ @w@o@r@d

len()

重点记忆

返回对象(字符、列表、元组等)长度或项目个数

str = 'hello word'
print(len(str))
# 10

strip(chars) & lstrip() & rstrip()

用于截掉字符串左右两边的空格或指定字符chars的所有组合。

用于截掉字符串左边的空格或指定字符chars的所有组合。

用于截掉字符串右边的空格或指定字符chars的所有组合。

str = '$$$$hello$word$$$$'
print(str.strip('$'))
print(str.lstrip('$'))
print(str.rstrip('$'))
str = 'helloword'
print(str.strip('eh'))
# hello$word
# hello$word$$$$
# $$$$hello$word
# lloword

max() & min()

方法返回字符串中最大的字母

方法返回字符串中最小的字母

str = 'helloword'
print(max(str))
print(min(str))
# w
# d

replace(old, new[, count])

把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数count,则最多替换count次。

str = 'This is a dog'
print(str.replace('is','was',1))
print(str.replace('is','was',2))
print(str.replace('is','was',3))
# Thwas is a dog
# Thwas was a dog
# Thwas was a dog

split(sep=None,maxsplit=-1) & rsplit()

重点记忆

通过指定分隔符sep对字符串进行切片,可以指定maxsplit最多分割次数。返回一个列表。

类似split,区别是从后往前分

str = 'This is a dog'
print(str.split(' '))
print(str.split(' ',2))
print(str.rsplit(' ',2))
# ['This', 'is', 'a', 'dog']
# ['This', 'is', 'a dog']
# ['This is', 'a', 'dog']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值