一篇搞懂之字符串
2019-07-29 本文已影响0人
远航天下
# created 29.05.2019
# author = "hang"
"""
字符串:由独立字符组成的一个序列,通常包含在单引号('')双引号("")或三引号(""" """或''' ''')之中
"""
"""字符串基础"""
name = "china"
city = "beijing"
text_1 = "I Love China !"
text_2 = '''I Love China !'''
texr_3 = """I Love China !"""
print(name, city, text_1, text_2, texr_3, sep="\n")
print(text_1 == text_2 == texr_3)
# 字符串嵌套
stu = "I'm a student !"
# 三引号多行函数注释
def func():
"""
注释11111111111111111111111111
注释22222222222222222222222222
注释33333333333333333333333333
:return:
"""
pass
# python转义字符
print("\\") # \
print("\'") # '
print("\"") # "
print("\n") # 换行
print("\t") # 空格
print("\b") # 退格
print("\v") # 纵向制表符
print("---------------------------------------------------")
"""字符串的常用操作"""
my_name = "china"
print(my_name[0]) # 字符串索引
print(my_name[1:3]) # 字符串切片
for i in my_name: # 字符串遍历
print(i)
# my_name[0] = "ss" # 会报错,字符串是不可变对象
s = "C" + my_name[1:] # 通过切片实现
print(s)
ss = my_name.replace("c", "C") # 通过replace()函数实现
print(ss)
s_1 = ''
for i in range(0, 100):
s_1 += str(i) # 字符串拼接方法一: +=
print(s_1)
l = []
for i in range(0, 100):
l.append(str(i))
s_2 = ''.join(l) # 字符串拼接方法二:join()函数
print(s_2)
s_3 = "https://blog.csdn.net/weixin_42359464/article/details/80882549https"
res_1 = s_3.split("/") # 字符串分割函数:split()
print(res_1)
print("%%%%%%%%%%%%%%%%%%%")
print(s_3.splitlines())
res_2 = s_3.strip("https") # 去掉首位的字符串:strip()
print(res_2)
res_3 = s_3.lstrip("https") # 去掉开头的字符串:lstrip()
print(res_3)
res_4 = s_3.rstrip("https") # 去掉的结尾的字符串:rstrip()
print(res_4)
s_4 = " l am a great ! "
result_1 = s_4.strip() # 去掉2头的空格
print(s_4) # 原字符串保持不动
print(result_1) # 去掉空格后的字符串
result_2 = s_4.find("am") # 字符串查找find()
print(result_2)
result_3 = s_4.index("g") # 字符串查找index()
print(result_3)
s_5 = "china"
print(s_5.encode("utf-8")) # 字符串编码
print(type(s_5.encode("utf-8")))
s_6 = b"china"
print(s_6.decode("utf-8")) # 解码
print(type(s_6.decode("utf-8")))
print(len(s_5)) # 字符串长度
s_7 = "damao good"
print("---------------------------------------------------")
print(s_7.upper()) # 全部转换成大写
print(s_7.lower()) # 全部转换成小写
print(s_7.capitalize()) # 首字母大写
print(s_7.istitle()) # 判断是否首字母大写
print(s_7.isupper()) # 字母是否全是大写
print(s_7.islower()) # 是否全是小写
print(s_7.center(50, "*")) # 中间对齐
print(s_7.ljust(100, "*")) # 左对齐
print(s_7.rjust(100, "*")) # 右对齐
print(s_7.count("a")) # 计算字符出现的次数
print(s_7.expandtabs())
print(s_7.isalnum()) # 判断是否全是字母或数字,至少一个字符
print(s_7.isalpha()) # 判断是否全是字母,至少一个字符
print(s_7.isspace()) # 是否全是空白字符,至少一个字符
print(s_7.splitlines(keepends=True)) # 将字符串转成一个列表
print(s_7.swapcase()) # 大小写互换
print(s_7.isnumeric())
print(s_7.title()) # 标题
print(s_7.zfill(20)) # 左边补0,连原字符串在内一共20个字符长度
print(s_7.startswith("d")) # 以书面开头
print(s_7.endswith("good")) # 以书面结尾
print(s_7.partition("g"))
print(s_7.rpartition("o"))
"""aplit、rsplit、splitlines"""
print("---------------------------------------------------")
s_8 = " 12311 23 123 "
print(s_8.split("1")) # 以1为界限划分左右两边,形成列表
print(s_8.split(sep="1", maxsplit=1)) # 界限为1, 从做之右只划分一次
print(s_8.split("1")) # 不会压缩连续的分隔符
print(s_8.split()) # 不指定分隔符时,即以空格进行分割, 会去除左右两边多余的空格
print(s_8.split(maxsplit=1))
s_9 = " 1 2\n 3 \n"
print(s_9.split())
print(s_9.split(" "))
print(s_9.split("\t"))
print(s_9.split("\n"))
print(''.split("\n"))
s_10 = "ab c\n\nde fg\rkl\r\n"
print(s_10.splitlines())
print(s_10.splitlines(keepends=True))
"""join"""
print("---------------------------------------------------")
# 字符串
s_11 = "asjdlsaj"
print("_".join(s_11))
# 元组
t_1 = ("1", "2", "3") # 元组的元素必须是字符串
print("_".join(t_1))
# 集合,注意集合无序
s_1 = {"1", "2", "3"} # 列表的元素必须是字符串, 拼接后是无序的,也就说,每次得到的结果可能都不一样
print("_".join(s_1))
# 列表
l_1 = ['py','th','o','n'] # 列表的元素必须是字符串
print("".join(l_1))
# 字典
d_1 = {'name':"malongshuai",'gender':'male','from':'China','age':18}
print("_".join(d_1)) # 把所有的key连接在一起
"""strip、lstrip、rstrip"""
print(" abc ".strip()) # 去除左右那边的空格
print(" abc ".lstrip()) # 去除左边的空格
print(" abc ".rstrip()) # 去除右边的空格
print("ssddffss".lstrip("s")) # 去除左边的“s”
print("ssddffss".rstrip("s")) # 去除右边的“s”
# 移除字符列表中的字符
print("www.baidu.com".lstrip("wq."))
print("wwq.baidu.com".lstrip("wq."))
print("wwqa.baidu.com".lstrip("wq."))
print("www.baidu.com".strip("womq."))
"""字符串格式化"""
print("no data avaliable for peeson with id:{}, name:{}".format("1233", "demo")) # format()格式化
print("no data avaliable for peeson with id:%s, name:%s" %("1111", "xiaohang")) # % 站维护
print(f"no data avaliable for peeson with id: {123}, name: {'china'}") # 要注意字符串的单引号和双引号嵌套的问题
print("".join(map(str, range(1000))))
"""
总结:
1、python中字符串使用单引号、双引号和三引号表示,三者意义相同并没有什么区别,其中三引号字符串通常用在多行字符场景。
2、python字符串是不可变的(新版本的字符串+=是个例外)。
3、新版本的字符串变的高效了很多,可以放心使用。
4、python字符串格式化常常用在输出和日志等场景。
"""