1.字符串运算 1.1 拼接字符串 if __name__ == "__main__" : name = "张三" sex = "男" print ("使用 + 拼接 >> " , "姓名:" + name + " 性别:" + sex) print ("使用 print 格式化 >> 姓名:%s 性别:%s " % (name, sex)) format_var = "姓名:{} 性别:{} " .format (name, sex) print ("使用 format >> " , format_var)
1.2 重复字符串 if __name__ == "__main__" : name = "张三" print ("使用:* >>" , name * 3 )
1.3 截取字符串 if __name__ == "__main__" : str_var = "我的名字叫张三丰~" print ("截取前3个字符串 >>" , str_var[:3 ]) print ("从索引=5,开始截取 >>" , str_var[5 :]) print ("从索引=1,开始截取,到索引=5为止 >>" , str_var[1 :5 ]) print ("演示:[i:j:k]" , str_var[1 :8 :2 ])
1.4 原样输出 if __name__ == "__main__" : str_var = "我的名\n 叫张三丰 \n 哈哈哈" print ("str_var >>" , str_var) r_str_var = r"我的名\n 叫张三丰 \n 哈哈哈" print ("r_str_var >>" , r_str_var)
2. 大小写 2.1 首字母大写 if __name__ == "__main__" : str_var = "my name is liu qing hui" print ("所有字符串首字母大写 title:" , str_var.title()) print ("第一个字符串首字母大写 capitalize:" , str_var.capitalize())
2.2 全部大写 if __name__ == "__main__" : str_var = "my name is liu qing hui" print ("全部转大写:" , str_var.upper())
2.3 全部小写 if __name__ == "__main__" : str_var = "MY NAME IS LIU QING HUI" print ("全部小写 lower:" , str_var.lower()) print ("全部小写 casefold :" , str_var.casefold())
lower()
方法只对ASCII
字符有效,而casefold()
方法对所有Unicode
字符都有效.
2.4 大转小和小转大 if __name__ == "__main__" : str_var = "Hello,World" print ("原字符串:" , str_var) print ("大转小,小转大:" , str_var.swapcase())
3. 统计 3.1 字符串出现次数 if __name__ == "__main__" : str_var = "abcd,abcd,abcd" print ("字符串a出现的次数:" , str_var.count("a" )) print ("str_var[0:7]:" , str_var[0 :7 ]) print ("字符串a在str_var[0:7]出现的次数:" , str_var.count("a" , 0 , 7 ))
3.2 字符串长度 if __name__ == "__main__" : str_var = "Hi,张三丰" str_var2 = "Hi" str_var3 = "张三丰" print ("str_var长度:" , len (str_var)) print ("str_var2长度:" , len (str_var2)) print ("str_va3r长度:" , len (str_var3))
4. 查找 4.1 字符串是否存在
find()
: 不存在则返回-1
,存在则从返回第一个匹配的索引。
if __name__ == "__main__" : str_var = "Hello,World" print ("h是否在字符串中:" , str_var.find("h" )) print ("o是否在字符串中:" , str_var.find("o" ))
index()
和find()
方法一样,区别是:当子字符串不存在时,index()
会报异常,推荐使用find()
4.2 使用 if __name__ == "__main__" : str_var = "Hello,张三" print ("H in str_var >>" , "H" in str_var) print ("H not in str_var >>" , "H" not in str_var) print ("张三 in str_var >>" , "张三" in str_var)
5. 判断 5.1 是否只包含字符或数字 if __name__ == "__main__" : str1 = "hello" print ("str1:" , str1.isalnum()) str2 = "hello123" print ("str2:" , str2.isalnum()) str3 = "hello@123" print ("str3:" , str3.isalnum()) str4 = "hello张三" print ("str4:" , str4.isalnum())
5.2 是否只包含字符 if __name__ == "__main__" : str1 = "hello" print ("str1:" , str1.isalpha()) str2 = "hello123" print ("str2:" , str2.isalpha()) str3 = "hello@123" print ("str3:" , str3.isalpha()) str4 = "hello张三" print ("str4:" , str4.isalpha())
5.3 是否只包含数字字符(0-9) if __name__ == "__main__" : str1 = "123456@" print ("str1:" , str1.isdigit()) str2 = "123456" print ("str2:" , str2.isdigit()) str3 = "张三18" print ("str3:" , str3.isdigit())
5.4 是否只包含数字字符 if __name__ == "__main__" : str1 = "123456" print ("str2:" , str1.isnumeric()) str2 = "一2三4五" print ("str2:" , str2.isnumeric())
5.5 是否有空白符
空白字符包括空格、制表符、换行符和回车符等
if __name__ == "__main__" : str1 = " " print ("str1:" , str1.isspace()) str2 = "\t\n" print ("str2:" , str2.isspace()) str3 = "hello world " print ("str3:" , str3.isspace())
5.6 是否首字母大写 if __name__ == "__main__" : str1 = "Hello world" print ("str1:" , str1.istitle()) str2 = "Hello World" print ("str2:" , str2.istitle())
6.删除和替换 6.1 替换 if __name__ == "__main__" : str1 = "a-b-a-b-a-b-a-b" print ("全部替换结果:" , str1.replace("a" , "@" )) print ("从左往右替换结果1次:" , str1.replace("a" , "@" , 1 )) print ("从左往右替换结果2次:" , str1.replace("a" , "@" , 2 )) print ("从左往右替换结果4次:" , str1.replace("a" , "@" , 4 ))
6.2 删除 if __name__ == "__main__" : str1 = " 1 # abc@ " print ("删除左右空白:" , str1.strip()) print ("删除右边空白:" , str1.rstrip()) print ("删除左边空白:" , str1.lstrip())
7.分割和拼接 7.1 分割 if __name__ == "__main__" : str = "Hello,World" print ("res: " , str .split("," ))
7.2 拼接 if __name__ == "__main__" : list_var = ["a" , "b" , "c" , "d" ] print ("res: " , "|" .join(list_var)) str1 = "abcd" print ("res2: " , "-" .join(str1))
8.格式化 if __name__ == "__main__" : format1 = "姓名:{} 年龄:{}" print (format1.format ("张三" , 18 )) format2 = "姓名:{1} 年龄:{0}" print (format2.format ("张三" , 18 )) format3 = "姓名:{0}{0} 年龄:{1}" print (format3.format ("张三" , 18 )) format4 = "姓名:{name} 年龄:{age}" print (format4.format (age=18 , name="张三" ))