python内置函数系列之str(三),持续更新。

python内置函数之str(三),持续更新。

11. index

介绍:

"""
S.index(sub[, start[, end]]) -> int
        
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.
        
Raises ValueError when the substring is not found.
"""

翻译:
S.index(sub[, start[, end]]) -> int 返回 S 中找到子字符串 sub 的最低索引,使得 sub 包含在 S[start:end] 中。可选参数 start 和 end被解释为切片表示法。当找不到子字符串时引发 ValueError。

简单来说:
你传入一个字符(串),返回它出现的第一次的第一个字符的索引。
你也可以控制查找区间。

index方法与find方法的区别:
使用index查找,没找到时会引发错误ValueError。
而使用find方法查找,没找到时返回 -1。
个人根据需求使用。

  • 参数:
  1. sub ---- 需要查找的字符(串)。
  2. start ---- 开始查找的位置,默认为0。
  3. end ---- 结束查找的位置,默认为最后。
  • 返回值:
    一个整形数字,代表发现该字符(串)的第一个位置索引。若没找到,引发错误ValueError。

代码示例:

a = "snfsahgasngk你好asajl21a4哈哈哈sfmlkkp124"
y = a.index('21', 5, 100)  # 特别注意:区间为左闭右开
x = a.index('666')
print(y)
# print(x) # 结果:    x = a.index('666')
#                     ValueError: substring not found
'''结果:
19
'''

12. isalnum

介绍:

"""
Return True if the string is an alpha-numeric string, False otherwise.
        
A string is alpha-numeric if all characters in the string are alpha-numeric and
there is at least one character in the string.
"""

即:
如果字符串是字母字符串,则返回 True,否则返回 False。如果字符串中的所有字符都是字母并且字符串中至少有一个字符,则字符串就是字母。

  • 参数:
    无。
  • 返回值:
    True:代表该字符串中的所有字符都是字母。
    False:代表该字符串中含有非字母的字符。

代码示例:

a = "snfsahgasngkasajl21a4哈哈sfmlkkp124"
b = "sjkhfiuasfk"
y = a.isalpha()
x = b.isalpha()
print(y)
print(x)
'''结果:
False
True
'''

类比其它几个方法:

  1. isascii
    如果字符串中的所有字符都是 ASCII,则返回 True,否则返回 False。

  2. isdecimal
    如果字符串是十进制字符串,则返回 True,否则返回 False。

  3. isdigit
    如果字符串是数字字符串,则返回 True,否则返回 False。
    (以下都在True的范围:
    Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字)

  4. isnumeric
    如果字符串是数字字符串,则返回 True,否则返回 False。
    (注:以下都在True的范围:Unicode数字,,全角数字(双字节))

  5. isidentifier
    如果字符串是有效的 Python 标识符,则返回 True,否则返回 False。

  6. islower
    如果字符串是小写字符串,则返回 True,否则返回 False。

  7. isupper
    如果字符串是大写字符串,则返回 True,否则返回 False。

  8. isspace
    如果字符串是空白字符串,则返回 True,否则返回 False。

13. join

此方法为字符串拼接,十分重要,用途广泛

介绍:

"""
Concatenate any number of strings.
        
The string whose method is called is inserted in between each given string.
The result is returned as a new string.

Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
"""

即:
连接任意数量的字符串。调用其方法的字符串插入到每个给定字符串之间。结果作为新字符串返回。示例:’.’.join([‘ab’, ‘pq’, ’ rs’]) -> ‘ab.pq.rs’

  • 参数:
    sequence:要连接的元素序列。(可以为列表、元组、字典、字符串、集合等)
  • 返回值:
    拼接完成的字符串。

代码示例:

b = ("1", "2", "3")
c = "## && "
d = ['hello', '333']  # 无论哪种对象,内部子对象必须为字符串形式
e = {'666', 'nihao'}  # 传入集合时,返回的拼接结果是无序的,这是由于集合的无序性导致的。
f = {"name": "john", 'address': 'shanghai'}  # 传入字典时,默认拼接的是key值
# 也可以修改传入的形式,改为拼接value值:
n = c.join(f.values())
x = c.join(b)
y = c.join(d)
z = c.join(e)
m = c.join(f)
print(x)
print(y)
print(z)
print(m)
print(n)
'''结果:
1## && 2## && 3
hello## && 333
666## && nihao
name## && address
john## && shanghai
'''

拼接字符串在以后的工作中十分常见,至于如何设计拼接,看需求。

14. replace

介绍:

"""
Return a copy with all occurrences of substring old replaced by new.
        
count
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
        
If the optional argument count is given, only the first count occurrences are replaced.
"""

即:

返回一个副本,其中所有出现的子字符串 old 被 new 替换。count 要替换的最大出现次数。-1(默认值)表示替换所有出现。如果给定可选参数 count,则只有第一次 count 出现换了。

  • 参数:
    old – 将被替换的子字符串。
    new – 新字符串,用于替换old子字符串。
    max – 替换次数(默认-1,即全部替换)。

  • 返回值:
    一个新的被替换了的字符串。

代码示例:

a = 'skkhg666ansljf1564klas1354666'

y = a.replace('k', '$$YYDS$$')
x = a.replace('666', '哈哈哈', 1)
print(y)
print(x)
'''结果:
s$$YYDS$$$$YYDS$$hg666ansljf1564$$YYDS$$las1354666
skkhg哈哈哈ansljf1564klas1354666
'''

15. split

介绍:

"""
Return a list of the words in the string, using sep as the delimiter string.
        
sep
	The delimiter according which to split the string.
    None (the default value) means split according to any whitespace,
    and discard empty strings from the result.
maxsplit
    Maximum number of splits to do.
    -1 (the default value) means no limit.
"""

即:

返回字符串中的单词列表,使用 sep 作为分隔符字符串。 sep 分割字符串的分隔符。无(默认值)表示根据任何空格进行分割,并从结果中丢弃空字符串.maxsplit 要进行的最大拆分次数。-1(默认值)表示没有限制。

简单来说:
split() 通过指定分隔符对字符串进行切片,如果第二个参数 num 有指定值,则分割为 num+1 个子字符串。

  • 参数:
    str – 分隔符,默认为所有的空字符。
    num – 分割次数。默认为 -1, 即分隔所有。

  • 返回值:
    返回分割后的字符串列表。

代码示例:

a = 'skkhg***ans*ljf1564**klas13*54666'

x = a.split('*', 4)  # 当有多个分隔符连在一起时,会丢弃一个空字符串,仔细观察结果。

print(x)
'''结果:
['skkhg', '', '', 'ans', 'ljf1564**klas13*54666']
'''
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值