day6 字符串

时间:2023-03-09 05:24:44
day6 字符串
重复输出字符串
 # * 重复输出字符串
print("hello"*2)

  

字符串切片

 # 字符串也拥有索引,和列表切片操作类似
print("helloworld"[2:])

  

判断字符串中是否包含某元素

 # 判断字符串中的字符是否存在,列表也可以使用此操作
print("e" in "hello")

  

较为常用的格式化输出

 print("yangtuo is a good boy")
print("%s is a good boy" %"yangtuo") print(st.format_map({"name":"alex","age":18} ))
# 另一种形式的格式化输出,比较麻烦不如上面那种方便,字典形式键值对传值

字符串拼接
 a = ""
b = "abd"
c = a + b
print(c) # 效率非常低很不推荐使用,需要开辟多块内存
# c = 123abc
 # "".join() 字符串的调用方法 链接字符串
c="***".join([a,b,d]) # 拼接用可以直接用""空字符串链接也可以
print(c) # 效率更好一些

  

字符串的其他所有方法

 #* 没用,比较废物的
## 较难用,需要注意
### 重点使用或者后期常用 st = "hello kitty {name} is {age}" print(st.count("l")) ### 计数某个字符的出现频率
print(st.capitalize()) # 字符串的首字符大写
print(st.center(50,"-")) # 指定字符串居中,然后用参数字符填充满指定数量
print(st.endswith("{age}")) ## 查看是不是以指定字符结尾
print(st.startswith("y")) ### 查看是不是以指定字符开始,在文件检索的时候很用得上
print(st.expandtabs(tabsize=10)) #* 控制字符串中搞得空格数量的
print(st.find("wwww")) ### 寻找到第一个字符,并返回他的索引值
print(st.rfind("wwww")) # 从右往左寻找到第一个字符,并返回他的索引值
print(st.format(name = "alex",age = "")) ###另一种形式的格式化输出,更加美观 ps:{}怎么解决??
print(st.format_map({"name":"alex","age":18} )) #*另一种形式的格式化输出,比较麻烦不如上面那种方便,字典形式键值对传值
print(st.index("wwww")) ###用法同find ,与find 的区别:find 如过找不到不会报错,会返回-1
print("abc456阳".isalnum()) #* 判断这个字符串是不是包括了数字或者字母,但是不能特殊字符
print("abc456阳".isalpha()) #* 判断这个字符串是不是只包含字母
print("".isdecimal()) #* 判断这个字符串是不是十进制的数
print("".isdigit()) # 判断是不是整型数字
print("SSSnnn".isidentifier()) #* 判断是不是标识符,判断变量名是不是合法
print("SSsss".islower()) # 判断是不是由小写字符组成
print("SSsss".isupper()) # 判断是不是由大写字符组成
print(" ".isspace()) # 判断是不是个空格
print("My Title ".istitle()) # 判断是不是个标题,标题格式为每个单词首字母大写
print("My Title ".lower()) ### 所有字符大写变小写
print("My Title ".upper()) ### 所有字符小写变大写
print("My Title ".swapcase()) # 大小写翻转,
print("My Title ".ljust(50,"-")) # 用法类似center只是靠左
print("My Title ".rjust(50,"-")) # 用法类似center只是靠右
print("\n My Title ".lstrip()) #忽略左边空格换行符(),在文本操作中很重要
print("\n My Title ".rstrip()) #忽略右边空格换行符(),在文本操作中很重要
print("\n My Title ".strip()) ###忽略全部空格换行符(),在文本操作中很重要
print("My Title".replace("My","Your"))### 替换内容
print("My Title shsi".split(" ")) ###从左往右将字符串按照指定参数为间隔分割成列表,可以再用join重新组成字符串
print("My Title shsi".rsplit(" ,1")) #*从右往左将字符串按照指定参数为间隔分割成列表,可以再用join重新组成字符串
print("My Title shsi".title()) #*将字符串按照首字符格式统一

重点的需要掌握的方法 
 print(st.count("l"))                               ### 计数某个字符的出现频率
print(st.startswith("y")) ### 查看是不是以指定字符开始,在文件检索的时候很用得上
print(st.find("wwww")) ### 寻找到第一个字符,并返回他的索引值
print(st.index("wwww")) ### 用法同find ,与find 的区别:find 如过找不到不会报错,会返回-1
print(st.format(name = "alex",age = "")) ### 另一种形式的格式化输出,更加美观 ps:{}怎么解决??
print("My Title ".lower()) ### 所有字符大写变小写
print("My Title ".upper()) ### 所有字符小写变大写
print("\n My Title ".strip()) ### 忽略全部空格换行符(),在文本操作中很重要
print("My Title".replace("My","Your")) ### 替换内容
print("My Title shsi".split(" ")) ### 从左往右将字符串按照指定参数为间隔分割成列表,可以再用join重新组成字符串
c="***".join(["ad","bc"]) ### 拼接用可以直接用""空字符串链接也可以