欢迎访问宙启技术站
智能推送

Python中的内置字符串函数介绍

发布时间:2023-06-21 00:56:54

Python中的字符串函数是为了对字符串进行操作的预设函数。Python中的所有字符串都是unicode字符,也就是说,它们可以包含所有的字符集,包括ASCII,拉丁字母等等。

这里将介绍一些常用的Python内置字符串函数:

1. upper() 和 lower()

upper() 和 lower()函数分别将字符串中的所有字母转换为大写或小写。

str = "Hello World"
print(str.upper())   # 输出 "HELLO WORLD"
print(str.lower())   # 输出 "hello world"

2. capitalize() 和 title()

capitalize() 函数将字符串的 个字母大写,其余字母小写。而 title() 函数将字符串中所有单词的首字母大写。

str = "hello world"
print(str.capitalize())  # 输出 "Hello world"
print(str.title())       # 输出 "Hello World"

3. center() 和 ljust() 以及 rjust()

这三个函数都是用来返回一个指定宽度的字符串。

- center(width, fillchar)返回一个居中对齐的字符串,并使用fillchar(默认值是空格)进行填充。

str = "hello"
print(str.center(10))         # 输出 "  hello   "
print(str.center(10, '-'))    # 输出 "--hello---"

- ljust(width, fillchar) 返回一个左对齐的字符串,并使用fillchar(默认值是空格)进行填充。

str = "hello"
print(str.ljust(10))          # 输出 "hello     "
print(str.ljust(10, '-'))     # 输出 "hello-----"

- rjust(width, fillchar) 返回一个右对齐的字符串,并使用fillchar(默认值是空格)进行填充。

str = "hello"
print(str.rjust(10))          # 输出 "     hello"
print(str.rjust(10, '-'))     # 输出 "-----hello"

4. strip() 和 lstrip() 以及 rstrip()

这三个函数都是用来去掉字符串两端的空白字符的。

- strip(chars) 返回一个去掉了字符串左右两端的指定字符(默认是空白字符)的字符串。

str = "   hello   "
print('|' + str.strip() + '|')  # 输出 "|hello|"
print('|' + str.strip('h') + '|')  # 输出 "|   hello   |"

- lstrip(chars) 返回一个去掉了字符串左边指定字符(默认是空白字符)的字符串。

str = "   hello   "
print('|' + str.lstrip() + '|')  # 输出 "|hello   |"
print('|' + str.lstrip('h') + '|')  # 输出 "|   hello   |"

- rstrip(chars) 返回一个去掉了字符串右边指定字符(默认是空白字符)的字符串。

str = "   hello   "
print('|' + str.rstrip() + '|')  # 输出 "|   hello|"
print('|' + str.rstrip('l') + '|')  # 输出 "|   hello   |"

5. find() 和 index()

这两个函数都是用来在字符串中查找某个子串的函数。他们的区别在于当查找不到时find() 函数返回 -1,而 index() 函数则抛出 ValueError 异常。

str = "hello world"
print(str.find('o'))    # 输出2 
print(str.find('w'))    # 输出6
print(str.find('ll'))   # 输出2
print(str.find('k'))    # 输出-1

print(str.index('o'))    # 输出2
print(str.index('w'))    # 输出6
print(str.index('ll'))   # 输出2
print(str.index('k'))    # 抛出 ValueError 异常

6. replace()

replace(old, new[, count]) 函数用于将字符串中的指定子串(old)替换为新串(new)。

可选参数count表示替换的最大次数,如果指定,则替换从左开始的最多count次。

str = "hello world"
print(str.replace('o', 'O'))    # 输出 "hellO wOrld"
print(str.replace('world', 'John'))    # 输出 "hello John"
print(str.replace('l', 'L', 1))    # 输出 "heLlo world"

7. split() 和 join()

这两个函数都是用来在字符串中处理分隔符。

- split(sep=None, maxsplit=-1) 函数通过指定分隔符对字符串进行切片,返回字符串列表。

可选参数maxsplit表示分割次数,分割次数大于指定次数时,分割将停止。

str = "hello world"
print(str.split())    # 输出 ["hello", "world"]
print(str.split('l'))  # 输出 ["he", "", "o wor", "d"]
print(str.split('l', 1))  # 输出 ["he", "lo world"]

- join(iterable) 函数用于将序列中的元素以指定分隔符连接生成一个新的字符串。

str = "hello world"
lst = str.split()
print('-'.join(lst))    # 输出 "hello-world"

8. isdigit() 和 isalpha()

这两个函数都是用来判断字符串内的字符是否符合指定类型的。

- isdigit() 函数用于判断字符串是否只包含数字字符。

str1 = "123"
str2 = "123abc"
print(str1.isdigit())    # 输出 True
print(str2.isdigit())    # 输出 False

- isalpha() 函数用于判断字符串是否只包含字母字符。

str1 = "abc"
str2 = "123abc"
print(str1.isalpha())    # 输出 True
print(str2.isalpha())    # 输出 False

以上是Python内置的一些常用字符串函数,这些函数可以有效帮助我们对字符串进行常用操作,根据需求选择相应函数即可。