日积月累学 Python-数据类型-String

String

使用单引号或者双引号

strip

删除指定字符串左右的字符, 默认删除空格

user_name = "  Tony  "
print(user_name.strip())  # 默认删除空格

user_name = "**Tony**"
print(user_name.strip("*"))  # Tony 删除指定的字符

与之相似的还有

  • lstrip 删除左边指定的字符
  • rstrip 删除右边指定的字符

center

填充至指定的长度, 原来的字符居中

user_name = "Tony"
print(user_name.center(12, '*'))  # ****Tony****

参数:

  • 参数1: 填充之后的长度
  • 参数2: 填充使用的字符, 默认是 空格

与之相似的还有

  • ljust: 原来的字符左对齐
  • rjust 原来的字符右对齐

Example

print('+'.center(37, "+"))
print('|', "user_name".center(15), '|', user_name.center(15), "|")
print('|', "user_age".center(15), '|', user_age.center(15), "|")
print('|', "user_sex".center(15), '|', user_sex.center(15), "|")
print('+'.center(37, "+"))
+++++++++++++++++++++++++++++++++++++
|    user_name    |       Tony      |
|     user_age    |        18       |
|     user_sex    |       nan       |
+++++++++++++++++++++++++++++++++++++

zerofill

用 0 填充字符串到指定的长度

age = "12"
print(age.zfill(10))  # 0000000012

参数:

  • 参数 1 : 填充之后的长度

split

用一个字符串取分隔另一个字符串

user_info = "Tony 18 man"
print(user_info.split(" "))  # ['Tony', '18', 'man']

参数:

  • 参数 1 : 用来分隔的字符
  • 参数 2 : 指定分隔的次数, 默认去全部

与之相似的函数

  • lsplit 从左边开始分隔
  • rstrip 从右边开始分隔

splitlines

按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

str1 = 'ab c\n\nde fg\rkl\r\n'
print(str1.splitlines())  # ['ab c', '', 'de fg', 'kl']
print(str1.splitlines(True))  # ['ab c\n', '\n', 'de fg\r', 'kl\r\n']

partition

用指定字符串取分隔字符串, 如果指定字符串存在, 则返回一个包含三个元素的元组, 第一个元素是分隔字符左边的字符串, 第二个元素是用来分隔的字符串; 第三个元素是分隔字符右边的字符串

web_url = "www.demo.com"
print(web_url.partition('.'))  # ('www', '.', 'demo.com')
print(web_url.rpartition('.'))  # ('www.demo', '.', 'com')

与之相似的函数

  • rpartition 从右边开始分隔

startswith

判断字符串是否以指定的字符串开始

user_des = "My name is Tony"
print(user_des.startswith("My"))  # True

endswith

判断字符串是否以指定的字符串开始

user_des = "My name is Tony"
print(user_des.endswith("ny"))  # True

find

查找指定字符首次出现的位置; 未找到返回 -1;

print("Demodeom".find('a', 0, -1))  # 2

参数:

  • 参数1 : 查找的字符
  • 参数2: 开始位置, 默认 0
  • 参数3: 结束位置, 默认 字符串结尾

与之相似的函数

  • rfind 从字符串右边开始查找

index

与 find 函数相似; 不同点是: 如果 找不到会报错 ValueError: substring not found

print("Demodeom".index('a', 0, -1))  # 2

与之相似的函数

  • rindex 从字符串右边开始查找

count

统计字符串中指定字符串出现的次数

print("Demodeom".count('m'))  # 2

参数:

  • 参数1 : 统计的字符
  • 参数2: 开始位置, 默认 0
  • 参数3: 结束位置, 默认 字符串结尾

len

获取字符串长度

print(len('abc'))  # 3
print(len('巭孬'))  # 2
print(len("①"))  # 1

replace

字符串替换

d_m = "fuck you"
print(d_m.replace('fuck', '***'))  # *** you

参数:

  • 参数 1 : 将被替换的字符
  • 参数 2 : 用来替换的字符
  • 参数 3 : 替换的次数, 默认全部替换

capitalize

第一个单词首字母大写

article_title = "this is the title"
print(article_title.capitalize())  # This is the title

title

所有单词首字母大写

print(article_title.title())  # This Is The Title

lower

所有字母小写

print(article_title.lower())  # this is the title

upper

所有的字母全部大写

print(article_title.upper())  # THIS IS THE TITLE

swapcase

反转大小写; 即 大写转为小写, 小写转为大写

print('abcABC'.swapcase())  # ABCabc

expandtabs

将字符串中 tab 转换为空格, \t 默认为 8 个空格

str = "this is\tstring example....wow!!!";
print(str)  # this is   string example....wow!!!
print(str.expandtabs())  # this is string example....wow!!!
print(str.expandtabs(16))  # this is         string example....wow!!!

encode

以指定的格式编码

str = "this is string example....wow!!!"
str_utf8 = str.encode('utf-8')
print(str_utf8)  # b'this is string example....wow!!!'
print(str_utf8.decode("utf-8"))  # this is string example....wow!!!

decode

str = "this is string example....wow!!!"
str_utf8 = str.encode('utf-8')
print(str_utf8)  # b'this is string example....wow!!!'
print(str_utf8.decode("utf-8"))  # this is string example....wow!!!

format

字符串格式化

不设置指定位置,按默认顺序

site = "name: {}, url: {}"
print(site.format("Demodeom", "www.demo.com"))  # name: Demodeom, url: www.demo.com

设置指定位置

site = "name: {1}, url: {0}"
print(site.format("www.demo.com", "Demodeom"))  # name: Demodeom, url: www.demo.com

通过字典设置参数

site_str = "name: {name}, url: {url}"
site = {"name": "Demodoem", "url": "www.demo.com"}
print(site_str.format(**site))
# name: Demodoem, url: www.demo.com

通过列表索引设置参数 ("0" 是必须的)

site_str = "name: {0[0]}, url: {0[1]}"
site_list = ['Demodeom', 'www.demo.com']
print(site_str.format(site_list))
# name: Demodeom, url: www.demo.com

格式判断

如果符合指定的格式, 返回 True , 否则返回 False

  • isalnum : 只包含字母或数字
  • isalpha : 只包含字母
  • isdecimal : 只包含十进制数字
  • isdigit : 只包含数字
  • isnumeric : 所有的字符均为数字
  • isspace : 只包含空格
  • istitle : 所有的单词, 首字母大写
  • isupper : 所有的字母大写
  • islower : 所有的字母小写
本作品采用《CC 协议》,转载必须注明作者和本文链接
Demodeom
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
初级开发工程师 @ 研发部
文章
1
粉丝
0
喜欢
0
收藏
0
排名:3496
访问:39
私信
所有博文
博客标签
社区赞助商