python基础数据类型str相关方法

最近有感对于python的基础知识点应该有一个详细的回顾,因此从今天开始将写一些python基础知识点的文章。

字符串str类中的方法:

# --------------------------------------------------------------------------------------

# 字符串是不可变类型
s = "hello World!"
print(id(s))    # 49906696
s = s.capitalize()
print(id(s))    # 51245888

# --------------------------------------------------------------------------------------

# capitalize()方法将一个字符串的首字母大写,而这个字符串的其余字母都变成小写。
s1 = "i want to go home"
print(s1.capitalize())  # I want to go home
s2 = "i want to GO home"
print(s2.capitalize())  # I want to go home
# 如果字符串中出现中文也不会报错
s3 = "i want 回家"
print(s3.capitalize())  # I want 回家
# 如果这个字符串中的第一个字符是中文的,那么这个字符串不会有改变
s4 = "我 want 回家"
print(s4.capitalize())  # I want 回家

# islower()
# isupper()
# 字符串内有中文不会影响判断,也可以有特殊字符
s = "ab#c"
s1 = "aBc"
s2 = "a我c"
s3 = "ABC"
print(s.islower())
print(s1.islower())
print(s2.islower())
print(s3.islower())
print(s.isupper())
print(s1.isupper())
print(s2.isupper())
print(s3.isupper())

# casefold()将字符串内的每个英文字母都变成小写
# 同样出现中文也么的问题
s = "I want TO 回家 home"
print(s.casefold())     # i want to go home

# --------------------------------------------------------------------------------------

# center()方法,让字符串位于整个字符串的中间
# 如果在center()内只设定width那么默认以空格填充
# 如果设定了fillchar,那么已设定的字符填充
s = "play basketball"
print(s.center(30))  #        play basketball
print(s.center(30, "#"))    # #######play basketball########

# expandtabs()
# 默认将tab转化为8个空格,可以在方法内设定
s = "\tthis expandtabs what"
print(s.expandtabs())   #         this expandtabs what
print(s.expandtabs(16))   #                this expandtabs what

# --------------------------------------------------------------------------------------

# count()
s = "hello my friend here"
# 设定查找的字符,不设定其他值,默认查找整个字符串
print(s.count("h"))  # 2
# 设定查找的范围,起始和终止,用索引来表示
print(s.count("h", 0, 2))   # 1
# 也可以查找成组的字符串
print(s.count("he"))    # 2
# count()不支持查找中文
s1 = "我们是一样的,我们本质上是没有区别的"
print(s.count("我"))     # 0

# --------------------------------------------------------------------------------------

# encode()
s = "this is encode"
# 默认以utf-8编码
print(s.encode())   # b'this is encode'
s1 = "这是编码方法"
print(s1.encode())       # b'\xe8\xbf\x99\xe6\x98\xaf\xe7\xbc\x96\xe7\xa0\x81\xe6\x96\xb9\xe6\xb3\x95'
print(s1.encode("gbk"))  # b'\xd5\xe2\xca\xc7\xb1\xe0\xc2\xeb\xb7\xbd\xb7\xa8'
s2 = b'\xe8\xbf\x99\xe6\x98\xaf\xe7\xbc\x96\xe7\xa0\x81\xe6\x96\xb9\xe6\xb3\x95'
print(s2.decode())  # 这是解码方法
s3 = b'\xd5\xe2\xca\xc7\xb1\xe0\xc2\xeb\xb7\xbd\xb7\xa8'
print(s3.decode("gbk"))  # 这是解码的方法

# --------------------------------------------------------------------------------------

# endwith()
# startswith()
# 方法内可以加上字符串索引来控制范围
s = "this is endwith"
print(s.endswith("th"))  # True
print(s.startswith("th"))  # True

# 不支持全中文字符串
s1 = "这是以什么结尾方法"
print(s.endswith("法"))  # False
print(s.startswith("这"))
# 字符串里必须是英文和中文同时存在才能判断是否以中文开头或结尾
s2 = "这是什么 this is 中文"
print(s2.startswith("th"))  # False
print(s2.endswith("文"))     # True
print(s2.startswith("这是"))  # True

# --------------------------------------------------------------------------------------

# find()
# index()
# rfind()       # 从右边开始寻找,返回第一个符合条件的字符的索引,也就是符合条件字符中最大索引的一个
# rindex()      # 同上
s = "this is find method"
# 此方法也可指定查找的范围,通过索引的方法
# 此方法也支持查找中文
# 此方法与index()相似,但是index找不到会报错,此方法找不到目标会返回-1
print(s.find("find"))   # 8 返回第一个目标字符的索引或第一个子字符串的第一个字符的索引
print(s.find("i"))  #

# --------------------------------------------------------------------------------------

# format()
# 用来格式化字符串使用,原字符串中用占位符
s = "this {} format"
print(s.format("is")) # this is format

# format_map
s = "this {first} format_map {second}"
print(s.format_map({"first": "is", "second": "method"}))
# 输出为   this is format_map method

# --------------------------------------------------------------------------------------

# isalnum()
# 此方法用来判断一个字符串是否是只由数字和字母组成
# 字符串中字母可以为中文
s = "abc123"
s1 = "123"
s2 = "abc"
s3 = "这是中文123"
s4 = "this is dot ."
print(s.isalnum())      # True
print(s1.isalnum())     # True
print(s2.isalnum())     # True
print(s3.isalnum())     # True
print(s4.isalnum())     # False

# isalpha()
# 此方法用来判断一个字符串中是否只是由字母组成
s = "abc123"
s1 = "123"
s2 = "abc"
s3 = "这是中文123"
s4 = "this is dot"
print(s.isalpha())      # False
print(s1.isalpha())     # False
print(s2.isalpha())     # True
print(s3.isalpha())     # False
print(s4.isalpha())     # False

# isdecimal()
# isdigit()
# isnumeric()
# 判断字符串中是否是十进制的数
s = "123"
s1 = "壹贰叁"
s3 = "12.04"
print(s.isdecimal())    # True
print(s1.isdecimal())   # False
print(s3.isdecimal())   # False

print(s.isdigit())
print(s1.isdigit())
print(s3.isdigit())

# 判断字符串中是否是数字,中文的数字也能认识,很神奇
s = "123"
s1 = "壹贰叁"
s3 = "12.04"
print(s.isnumeric())
print(s1.isnumeric())
print(s3.isnumeric())

# isidentifier()
# 判断一个字符串是否可以当作变量,是否符合变量的命名规则
s = "123"
s1 = "123what"
print(s.isidentifier())
print(s1.isidentifier())

# isprintable()
# 判断一个字符串内是否含有不可打印出来的字符
s = "what\t"
print(s.isprintable())  # False

# isspace()
# 判断一个字符串是否全部由空格组成,如果是返回True,否则返回False
# 换行符 TAB键认为是空格
s = "  "
s1 = "w h a t"
s3 = "\t\t\n"
print(s.isspace())  # True
print(s1.isspace()) # False
print(s3.isspace()) # True

# istitle()
# 判断一个字符串是否是标题形式,即每个单词的首字母大写
# 如果每个单词的首字母大写,则为True,否则为False
s = "I am here"
s1 = "I Am Here"
s2 = "I Am here"
print(s.istitle())      # False
print(s1.istitle())     # True
print(s2.istitle())     # False

# --------------------------------------------------------------------------------------

# join()
# 用一个字符串将一个可迭代对象连接成一个字符串,而这个可迭代对象不能有int型元素
lst = [1, 2, 3]
lst2 = ["what", "is", "this"]
lst3 = ["what", 1, "this"]
# s = "fuck".join(lst)   # TypeError: sequence item 0: expected str instance, int found
s = "-fuck-".join(lst2)
print(s)      # what-fuck-is-fuck-this
# s2 = "-fuck-".join(lst3) # TypeError: sequence item 0: expected str instance, int found

tup = ("what", "world")
print("hello".join(tup))     # whathelloworld

# ljust()
# rjust()
# 字符串填充,用一个指定的字符,指定一定的数量
# left 和 right指的的这个字符串处于新字符串左边还是右边
s = "what"
print(s.ljust(10, "*"))     # what******
print(s.rjust(10, "*"))     # ******what

# strip()
# lstrip()  从左边去除
# rstrip()  从右边去除
# 方法内不加参数,默认去除字符串两边的空格,换行符等
# 方法内加上参数,可去除指定的元素
s = "  what is  this   "
print(s.strip())    # what is  this
s1 = "fuck this is the fuck"
print(s.strip("fuck"))  #   what is  this

# --------------------------------------------------------------------------------------

# maketrans()
# 将指定的字符转化为一个字典,字典中是ASCII码对应数值,如下
# 如果是两个参数必须是等长的,如果是一个参数必须是个字典
s = "this is maketrans"
print(s.maketrans("th", "TH"))     # {116: 84, 104: 72}
print(s.maketrans({"t": "fuck"}))   # {116: 'fuck'}
print(s.maketrans("t", "i", "ra"))   # {116: 105, 114: None, 97: None}
maktrans 是一个静态方法,用于生成一个对照表,以供 translate 使用。
# 该方法为了translate使用,用于变换字符串,类似于替代的作用。

# translate()
# 此方法用于字符串的变换
s = "this is translate"
table = s.maketrans("is", "fu")
print(s.translate(table))   # thfu fu tranulate

# --------------------------------------------------------------------------------------

# partition()
# rpartition()
# 用于字符串分组,相当于切割,得到的结果是个元组
s = "this is partition"
print(s.partition("is"))    # ('th', 'is', ' is partition')
print(s.partition("th"))    # ('', 'th', 'is is partition')

# --------------------------------------------------------------------------------------

# replace()
# 此方法用于替换字符串中的指定的字符,默认全部替换,也可指定替换的次数
s = "i love python"
print(s.replace("python", "fuck"))  # i love fuck

# --------------------------------------------------------------------------------------

# split()
# rsplit()
# 此方法用于分割字符串,返回的是一个列表,不加参数默认为以空格分隔
s = "this is split"
print(s.split())    # ['this', 'is', 'split']
print(s.split("is"))    # ['th', ' ', ' split']
print(s.rsplit("th"))   # ['', 'is is split']

# --------------------------------------------------------------------------------------

s = '''
split()
rsplit()
此方法用于分割字符串返回的是一个列表
s = "this is split"
print(s.split())    # ['this', 'is', 'split']
print(s.split("is"))    # ['th', ' ', ' split']
print(s.rsplit("th"))   # ['', 'is is split']
'''

print(s.splitlines())   # 返回的是一个列表,包含每一行的信息,不包含换行符等

# --------------------------------------------------------------------------------------

# swapcase()
# 此方法用于大小写转换
s = "this IS the FUn thing"
print(s.swapcase())     # THIS is THE fuN THING

# --------------------------------------------------------------------------------------

# zfill()
# 此方法用于在字符串开始位置填充,0, 使字符串中元素个数达到指定的个数。
s = "this is zfill"
print(s.zfill(20))  # 0000000this is zfill

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值