python基础:Str(字符串)

发布时间 2023-10-27 00:12:16作者: 文刀六日

字符串

  1. 字符串创建 address = 'shanghaishipudong xinqu' wenhouyu = 'Hello, Jean'
  2. 字符串拼接 str_new = address + wenhouyu
  3. 字符串转换 字符串的数字转 int\float; 数字类型转文本 str
    ** str1 = '123' a = int(str1)
    ** str2 = 'hello'+ str(456)
  4. 大小写转换
    'bwm'.upper();
    'BWM'.lower();
    'how are you'.capitalize(); 首字母大写
    'hell cherry liu'.title(); 每个单词首字母大写
    'abcDFG'.swapcase(); 大小写互换
  5. 制表符换行符 \t \n
  6. 删除空格
    ** space_str = ' hello world '
    ** len(space_str) 字符长度
    ** space_str.strip() 左右空格都去掉
    ** space_str.lstrip() 左空格去掉
    ** space_str.rstrip() 右空格去掉
  7. 字符串查找 find(寻找字符,开始索引,结束索引)
    find_str = 'Hello,Cherry.Liu, How are you'
    find_index = find_str.fine('o')
    is_exist = o in find_str o在字符串中是否存在
  8. 字符串替换replace(元数据,替换数据,次数)
    replace_str = 'Hello,Cherry.Liu, How are you'
    replace_str.replace('l','L')