zoukankan      html  css  js  c++  java
  • 09 python初学 (字符串)

    # 重复输出字符串
    print('hello' * 2)
    # >>>hellohello
    
    # 字符串切片操作,最重要的!!!!
    print('hello'[2:])
    # >>>llo
    
    # 关键字 in
    print('ll' in 'hello')
    # >>> True
    
    # 字符串拼接
    
    # 不推荐使用此种方式,方式一
    a = '123'
    b = 'abc'
    c = 'haha'
    d = a + b
    print(d)
    # >>> 123abc
    # 方式二join,字符串的拼接: '拼接字符串'.join([a, b]),将后面列表里的a,b用前面的拼接字符串拼接起来
    d = ''.join([a, b])
    print(d)
    # >>>123abc
    d = '---'.join([a, b, c])
    print(d)
    # >>>123---abc---haha

     整理字符串中重要的常用方法:

    st = 'hello world'

    1. 字符串切片操作:
      print('hello'[2:])
    2. count方法:统计字符串中某个元素个数
      print(st.count('l'))   3
    3. center 方法:字符串居中,一共 50 个字符,字符串左右两边用 ‘-’ 填充
      print(st.center(50, '-'))
      # -------------------hello world--------------------
    4. startwith、endwith:判断字符串是否以某内容开头、结尾,返回一个布尔值
      print(st.startswith("hel"))     True
    5. find 方法:返回该内容所在的第一个索引值。
      print(st.find('lo'))
      注:同样的还有一个 index()方法,区别在于当要查询的内容不在字符串中时,index 方法会报错,find 方法会返回 -1
    6. format 方法:字符串格式化输出
      st1 = 'hello\tworld {name} {name1}'
      print(st1.format(name="lily", name1="su"))
      同样还有一个 format_map() 方法,这两种区别主要在于参数用什么方式去写。
      print(st1.format_map({'name': "lily", "name1": "su"}))  # 参数用字典形式
    7. lower、upper 方法:将字符串所有都变成小写(大写)。
      print('My Title'.lower())
    8. strip:去掉字符串左右空格、换行符、制表符。lstrip 只去掉左边的,rstrip 去掉右边的。
      print('    My Title   \n    '.strip())         #   My Title
    9. replace 方法:替换内容。如果不写后面的 count 参数,默认全部进行替换。
      print('my title title'.replace('title', 'girl', 1))     #   my girl title
    10. split 方法:将字符串按某字符分隔开,存入列表中
      print('my title'.split(' '))    #  ['my', 'title']
    11. isdigit 方法:判断是不是一个整型,和 .isnumeric()一样的作用
      print('12345 '.isdigit())     #  True

    了解的方法:

    1.  capitalize()方法:将字符串首字母大写
      print(st.capitalize())     #  Hello world
    2. expandtabs()函数:设置 \t 空格长度
      print(st1.expandtabs(tabsize=10))     #   hello     world {name} {name1}
    3. isalnum()函数:判断字符串是否只包括数字或字母或汉字,返回布尔值。
      print('abc456'.isalnum()    # True
    4. isdecimal()函数:判断是不是一个十进制数字。
      print('12345'.isdecimal())     # True
    5. isidentifier()函数:检验是不是一个非法变量名称(变量名称不能以数字开头),返回一个布尔值
      print('34abc'.isidentifier())      # False
    6. islower()函数:检验字符串是不是全小写。isupper()检验字符串是不是全大写
      print('abc@'.islower())     # True
    7. isspace()函数:检验是不是空格,返回布尔值
      print('     '.isspace())   #  True
    8. istitle()函数:检验字符串是不是每个单词首字母大写
      print('My Title'.istitle())    # True
    9. title()函数:按照 title 格式对字符串进行修改
      print('My title'.title())     # My Title
    10. swapcase()函数:将字符串中的字母大小写反转
      print('My Title'.swapcase())     #  mY tITLE
    11. ljust()、rjust()函数:# 字符串靠左,一共10个字符,剩下的都由*补充   .rjust()字符串靠右,一共10个字符,剩下的都由*补充
      print('My Title'.ljust(10, '*'))     #  My Title**
  • 相关阅读:
    Elasticsearch学习系列之介绍安装
    Python学习系列之文件操作
    Python学习系列之异常处理
    Python学习系列之装饰器
    windows 修改xhsell安全加密配置
    ipv6nginx错误
    zabbix 硬盘状态收集,制作表格
    申请免费ssl证书
    gitlab 搭建与迁移
    mogilefsdBUG mogilefsd[15624]: crash log: Modification of a read-only value attempted at /usr/local/share/perl5/Sys/Syscall.pm line 227
  • 原文地址:https://www.cnblogs.com/mlllily/p/10159678.html
Copyright © 2011-2022 走看看