"""
Time = "2021-08-10"
Author = "Yblackd"
Desc = "python_String 常用方法练习"
"""
__Time__ = "2021-08-10" __Author__ = "Yblackd" __Desc__ = "python_String 常用方法练习" # !/usr/bin/env python # -*- coding: GBK -*- import random def str_case(): """字符串大小写转换""" s = "ThIs a Python" print("大写转换成小写: \t %s" % s.lower()) print("小写转换成大写: \t %s" % s.upper()) print("大小写转换: \t %s" % s.swapcase()) print("首字母大写: \t %s" % s.title()) pass def str_find(): """字符串搜索、替换、去除空格""" s = " ThIs is a Python " print("字符串搜索: \t %s" % s.find("is")) print("字符串统计: \t %s" % s.count("s")) print("字符串替换: \t %s" % s.replace("a", "an")) print("字符串去左右空格: \t %s" % s.strip()) print("字符串去 左 空格: \t %s" % s.lstrip()) print("字符串去 右 空格: \t %s" % s.rstrip()) def str_split(): """字符串分割、组合""" s = " ThIs is a Python " print("字符串分割 : \t %s" % s.split()) print("字符串组合1 : \t %s" % ("#".join(['ThIs', 'is', 'a', 'Python']))) print("字符串组合2 : \t %s" % (" ".join(['ThIs', 'is', 'a', 'Python']))) def str_code(): """字符串编码、解码""" s = "编解码测试" str_utf8 = s.encode("UTF-8") str_gbk = s.encode("GBK") print("UTF-8 编码:", str_utf8) print("GBK 编码:", str_gbk) print("UTF-8 解码:", str_utf8.decode('UTF-8', 'strict')) print("GBK 解码:", str_gbk.decode('GBK', 'strict')) def str_test(): """ 字符串测试 :return bool """ s = "abcd" s1 = "123a" s2 = "123" s3 = " " s4 = "abcde" s5 = "ABCDE" s6 = "Abcde" print("是否全是字母 \t%s" % s1.isalpha()) print("是否全是数字 \t%s" % s2.isdigit()) print("是否全是空白字符 \t %s" % s3.isspace()) print("是否全部小写 \t %s" % s4.islower()) print("是否全部大写 \t %s" % s5.isupper()) print("是否首字母大写 \t %s" % s6.istitle()) def test(): """ python中 列表中 放str类型的数字, print是不带双引号,想要将结果给C++,需要输出带双引号的值 :return: """ l1 = [] for i in range(100): l1.append(("\"%s\"" % str(i))) s1 = ", ".join(l1) print(s1) if __name__ == '__main__': # str_case() # str_find() # str_split() # str_code() # str_test() test()