Python 字符串 String 内建函数大全(2)

在上一篇文章中,我们提到了部分 Python 中字符串 string 的内建函数,这篇文章我们将继续介绍其他函数。

lower() 函数

功能

将字符串中的字母转换为小写

用法

str.lower()

参数

返回值

字符串

示例代码

str = "HELLO WORLD!"

print str.lower()

运行结果

hello world!

lstrip() 函数

功能

把字符串左边的特定字符全部截取掉,默认字符为空格

用法

str.lstrip([char])

参数

chars: 被截取掉的字符

返回值

返回截取后的字符串

示例代码

str = "    HELLO WORLD!"

print str.lstrip()

str = "!!!!!!Hello world!"

print str.lstrip('!')

运行结果

HELLO WORLD!

Hello world!

maketrans() 函数

功能

将字符串中的一部分字符替换成另一部分

用法

str.maketrans(intab, outtab)

参数

intab: 被替换的字符

outtab: 替换的字符

返回值

返回替换规则

示例代码

from string import maketrans

str = "abcdefghijk"

intab = "acrgik"

outtab = "123456"

trans = maketrans(intab, outtab)

print str.translate(trans)

运行结果

1b2def4h5j6

max(str) 函数

功能

返回字符串中最大的字符

用法

max(str)

参数

返回值

返回字符串中最大的字符

示例代码

str = "abcdefghijk"

print "MAX character: " + max(str)

str = "123abc"

print "MAX character: " + max(str)

运行结果

MAX character: k

MAX character: c

min 函数

功能

返回字符串中最小的字符

用法

min(str)

参数

返回值

返回字符串中最大的字符

示例代码

str = "abcdefghijk"

print "MIN character: " + min(str)

str = "123abc"

print "MIN character: " + min(str)

运行结果

MIN character: a

MIN character: 1

推荐下我自己创建的Python学习交流群923414804,这是Python学习交流的地方,不管你是小白还是大牛,小编都欢迎。

replace() 函数

功能

将字符串中的子字符串用某字符串来代替

用法

str.replace(old, new[, max])

参数

old: 被替换的子字符串

new: 替换后的字符串

max: 需要替换的个数

返回值

返回替换后的字符串

示例代码

str = "this is a string, this is a string"

print str.replace("is", "was")

print str.replace("is", "was", 2)

运行结果

hwas was a string, thwas was a string

thwas was a string, this is a string

split() 函数

功能

分割字符串

用法

str.split(str=" ", num=string.cout(str))

参数

str: 分隔符,默认是空格

num: 分割的次数,默认为按照分隔符分割整个字符串

返回值

返回分割后的 list

示例代码

str = "word1 word2 word3 word4"

print str.split();

print str.split('r')

print str.split(' ', 2)

运行结果

['word1', 'word2', 'word3', 'word4']

['wo', 'd1 wo', 'd2 wo', 'd3 wo', 'd4']

['word1', 'word2', 'word3 word4']

splitlines() 函数

功能

将字符串按行分割

用法

str.splitlines(num=string.count('\n'))

参数

num: 该数值如果不为0,表示分割后的字符串中保留\n

返回值

返回分割后的 list

示例代码

str = "line1\nline2\nline3\nline4"

print str.splitlines();

print str.splitlines(0);

print str.splitlines(2)

运行结果

['line1', 'line2', 'line3', 'line4']

['line1', 'line2', 'line3', 'line4']

['line1\n', 'line2\n', 'line3\n', 'line4']

startswith() 函数

功能

判断字符串是否是以某子字符串开头

用法

str.stratswith(str, start=0, end=len(str))

参数

str: 被检查的子字符串

start: 检查的字符串的起始 index,默认为 str 的开始位置

end: 检查的字符串的结束 index,默认为 str 的终止位置

返回值

如果字符串是否是以某子字符串开头,返回True;否则返回False

示例代码

str = "hello world!"

print str.startswith('hel')

print str.startswith('hel',2,8)

运行结果

True

False

strip() 函数

功能

去除字符串两边的某字符

用法

str.strip([char])

参数

char: 需要去除的字符

返回值

返回去除之后的字符串

示例代码

str = "!hello!!world!"

print str.strip('!')

运行结果

hello!!world

swapcase() 函数

功能

将字符串中的大小写字母转换

用法

str.swapcase()

参数

返回值

返回转换后的字符串

示例代码

str = "Hello World!"

print str.swapcase()

运行结果

hELLO wORLD!

upper() 函数

功能

将字符串中的字母都转换成大写

用法

str.upper()

参数

返回值

返回转换后的字符串

示例代码

str = "Hello World!"

print str.upper()

运行结果

HELLO WORLD!

至此,我们在 Python 中常见的、常用的字符串内建函数就大概都介绍过了,如果以后我又想起来一些会继续往上边添加的~

猜你喜欢

转载自blog.csdn.net/weixin_34283445/article/details/86952182