重复输出字符串
1 # * 重复输出字符串 2 print("hello"*2)
字符串切片
1 # 字符串也拥有索引,和列表切片操作类似 2 print("helloworld"[2:])
判断字符串中是否包含某元素
1 # 判断字符串中的字符是否存在,列表也可以使用此操作 2 print("e" in "hello")
较为常用的格式化输出
1 print("yangtuo is a good boy") 2 print("%s is a good boy" %"yangtuo") 3 4 print(st.format_map({"name":"alex","age":18} )) 5 # 另一种形式的格式化输出,比较麻烦不如上面那种方便,字典形式键值对传值
字符串拼接
1 a = "123" 2 b = "abd" 3 c = a + b 4 print(c) # 效率非常低很不推荐使用,需要开辟多块内存 5 # c = 123abc
1 # "".join() 字符串的调用方法 链接字符串 2 c="***".join([a,b,d]) # 拼接用可以直接用""空字符串链接也可以 3 print(c) # 效率更好一些
字符串的其他所有方法
1 #* 没用,比较废物的 2 ## 较难用,需要注意 3 ### 重点使用或者后期常用 4 5 st = "hello kitty {name} is {age}" 6 7 print(st.count("l")) ### 计数某个字符的出现频率 8 print(st.capitalize()) # 字符串的首字符大写 9 print(st.center(50,"-")) # 指定字符串居中,然后用参数字符填充满指定数量 10 print(st.endswith("{age}")) ## 查看是不是以指定字符结尾 11 print(st.startswith("y")) ### 查看是不是以指定字符开始,在文件检索的时候很用得上 12 print(st.expandtabs(tabsize=10)) #* 控制字符串中搞得空格数量的 13 print(st.find("wwww")) ### 寻找到第一个字符,并返回他的索引值 14 print(st.rfind("wwww")) # 从右往左寻找到第一个字符,并返回他的索引值 15 print(st.format(name = "alex",age = "37")) ###另一种形式的格式化输出,更加美观 ps:{}怎么解决?? 16 print(st.format_map({"name":"alex","age":18} )) #*另一种形式的格式化输出,比较麻烦不如上面那种方便,字典形式键值对传值 17 print(st.index("wwww")) ###用法同find ,与find 的区别:find 如过找不到不会报错,会返回-1 18 print("abc456阳".isalnum()) #* 判断这个字符串是不是包括了数字或者字母,但是不能特殊字符 19 print("abc456阳".isalpha()) #* 判断这个字符串是不是只包含字母 20 print("123456".isdecimal()) #* 判断这个字符串是不是十进制的数 21 print("123456".isdigit()) # 判断是不是整型数字 22 print("SSSnnn".isidentifier()) #* 判断是不是标识符,判断变量名是不是合法 23 print("SSsss".islower()) # 判断是不是由小写字符组成 24 print("SSsss".isupper()) # 判断是不是由大写字符组成 25 print(" ".isspace()) # 判断是不是个空格 26 print("My Title ".istitle()) # 判断是不是个标题,标题格式为每个单词首字母大写 27 print("My Title ".lower()) ### 所有字符大写变小写 28 print("My Title ".upper()) ### 所有字符小写变大写 29 print("My Title ".swapcase()) # 大小写翻转, 30 print("My Title ".ljust(50,"-")) # 用法类似center只是靠左 31 print("My Title ".rjust(50,"-")) # 用法类似center只是靠右 32 print("\n My Title ".lstrip()) #忽略左边空格换行符(),在文本操作中很重要 33 print("\n My Title ".rstrip()) #忽略右边空格换行符(),在文本操作中很重要 34 print("\n My Title ".strip()) ###忽略全部空格换行符(),在文本操作中很重要 35 print("My Title".replace("My","Your"))### 替换内容 36 print("My Title shsi".split(" ")) ###从左往右将字符串按照指定参数为间隔分割成列表,可以再用join重新组成字符串 37 print("My Title shsi".rsplit(" ,1")) #*从右往左将字符串按照指定参数为间隔分割成列表,可以再用join重新组成字符串 38 print("My Title shsi".title()) #*将字符串按照首字符格式统一
重点的需要掌握的方法
1 print(st.count("l")) ### 计数某个字符的出现频率 2 print(st.startswith("y")) ### 查看是不是以指定字符开始,在文件检索的时候很用得上 3 print(st.find("wwww")) ### 寻找到第一个字符,并返回他的索引值 4 print(st.index("wwww")) ### 用法同find ,与find 的区别:find 如过找不到不会报错,会返回-1 5 print(st.format(name = "alex",age = "37")) ### 另一种形式的格式化输出,更加美观 ps:{}怎么解决?? 6 print("My Title ".lower()) ### 所有字符大写变小写 7 print("My Title ".upper()) ### 所有字符小写变大写 8 print("\n My Title ".strip()) ### 忽略全部空格换行符(),在文本操作中很重要 9 print("My Title".replace("My","Your")) ### 替换内容 10 print("My Title shsi".split(" ")) ### 从左往右将字符串按照指定参数为间隔分割成列表,可以再用join重新组成字符串 11 c="***".join(["ad","bc"]) ### 拼接用可以直接用""空字符串链接也可以