公司动态
了解python中字符串及其操作
1. 定义Python 字符串就是用引号括起来的文本数据。在 Python 里它的类型是 str 字符串的声明 可以使用单引号双引号三引号 2. 转义 \ \ \ \ 使用引号失去声明字符串的作用 \n 换行 \t制表符缩进 \\ 第一个是转义字符 使第二个失去转义作用称为普通字符# msg 123qwe-*/#%……*、 如果字符串中有双引号则外侧用单引号 # print(msg) # msg2 123qwe-*/#%……*、 如果字符串中有单引号则外侧用双引号 # print(msg)msg4 hello\nworld\thi world \n 为换行 \t 是加入空格 print(msg4)msg5 C:\\Program Files\\Python313\\new 如果字符串中有\ 则在其前面再加一个 print(msg5)name qiku age 1 addr 东三街 print(名字叫, name, 年纪是, age, 地址是, addr, sep) # 使用f print(f名字叫{name}年纪是{age}地址是{addr}) # 使用%连接 print(名字叫%s年纪是%s地址是%s % (name, age, addr)) # 使用format函数 print(名字叫{}年纪是{}地址是{}.format(name, age, addr))# 转小写print(heLlo WorlD.lower())# 转大写print(heLlo WorlD.upper())# 单词首字母大写print(heLlo worlD.title())# 首字母大写print(heLlo worlD.capitalize())# 大小写转换print(heLlo worlD.swapcase())# 对齐# 居中print(hello world.center(20))print(hello world.center(20, *))# 居左print(hello world.ljust(20, *))# 居右print(hello world.rjust(20, *))# 居右 左侧填0print(hello world.zfill(20))# 开始结尾print( hello world .startswith( ))print( hello world .endswith( ))# 剔除空白print( hello world .strip())print(hello world.strip())print(hello world.lstrip())print(hello world.rstrip())# 拼接print(.join(hello world))print( .join([hello, world]))# 切割 默认空格print(hello world.split())print(hello world.split(l))# 替换 count 指明替换次数print(hello world hello china.replace(hello, hi, 1))# bytes 字符串前有字符 bprint(hello world 123 中国.encode(gbk))# 解码print(bhello world 123 \xd6\xd0\xb9\xfa.decode(gbk))# is*print(az.isalpha())print(19.isdigit())print(19az.isalnum())print(Az.islower())print(Az.isupper())print(Az Bz.istitle())