zoukankan      html  css  js  c++  java
  • Python学习---Python下[字符串]的学习

    字符串[不可变]

    重点方法: in  
               count()
               center(50,'*')
               startswith('h')
               endwith('h')
               find('r') : 找不到返回 -1
               index('r'): 找不到会报错
               lower()
               upper(')
               strip()
               replace('itle','lesson',1)
               split('i',1)

    实例:

    #  * 重复输出字符串
    print("hello	"*2)     # hello hello
    # 通过索引获取字符串中字符,这里和列表的切片操作是相同的
    print("hello"[2:4])    # ll
    #  in  成员运算符 - 如果字符串中包含给定的字符返回 True
    print('el' in 'hello') # True
    print('hello' in ['hello', 'world', '2017']) # True
    #  %   格式字符串, 字符注意添加引号
    print('%s is a  China Company' % 'Huawei') # Huawei is a  China Company
    # +   字符串拼接
    print('hello	'+"world2017") # hello   world2017
    # join  -->   + 效率低,该用join,join里面的必须是迭代器,所以必须是列表,元组等’
    # 字符串拼接用join   【列表变字符串】
    a = 'hello'
    b = 'world2018'
    # 用******连接a,b
    print('******'.join([a, b]))  # hello******world2018
    print(''.join(('hello	', 'world2019'))) # hello   world2019
    print(''.join(['hello	', 'world2019'])) # hello   world2019
    print('***'.join(('hello'.strip()+'I love'))) # h***e***l***l***o***I*** ***l***o***v***e
    print('******'.join(('hello', 'I love')))  # hello******I love
    
    # 字符串的切割,返回一个列表  【字符串变列表】
    print('hello world 2017'.split())  #  ['hello', 'world', '2017']
    print('hello world 2017'.rsplit('l',2))  # ['hel', 'o wor', 'd 2017']
    # 字符串的右切割,返回一个列表  【字符串变列表】
    print('hello world 2017'.rsplit('l'))  # ['he', '', 'o wor', 'd 2017']
    # 首字母大写
    print('hello'.capitalize())  # Hello
    #  string.count(str, beg=0, end=len(string))
    print('helloworldhelloworld'.count('l', 2, 22))  # 6
    # string.decode(encoding='UTF-8', errors='strict')     以 encoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的 异 常 , 除 非 errors 指 定 的 是 'ignore' 或 者'replace'
    # string.encode(encoding='UTF-8', errors='strict')     以 encoding 指定的编码格式编码 string,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace'
    
    # string.endswith(obj, beg=0, end=len(string))
    print('hello'.endswith('o'))  # True
    # string.startswith(obj, beg=0, end=len(string))
    print('hello'.startswith('h')) # True
    # string.expandtabs(tabsize=8), 把字符串 string 中的 tab 符号转为空格,默认是8
    print('h    ello'.expandtabs(tabsize=8)) # h    ello
    # string.find(str, beg=0, end=len(string)),找不到不报错,返回-1
    print('hello'.find('llo'))    # 返回2
    print('hello'.find('xyz'))    # 返回-1,表示查找不到
    # 从右开始匹配,返回的字符串原来的位置,真实的索引位置
    print('hello'.rfind('l'))  # 3
    # 查找字符所在的位置, 找不到会报错
    print('hello'.index('o')) # 4
    # 格式化输出 format
    print('hello {name} {year}'.format(name='world', year='2019')) # hello world 2019
    # 格式化输出 format_map,要求用字典表示
    print('hello {name} {year}'.format_map({'name': 'world', 'year': '{2020}'})) # hello world {2020}
    # 判读字符串是否包含数字和字母,至少一个字符  ==> Java里面的正则 w
    print('hello1235'.isalnum()) # True
    # 判断是否是个十进制的数字
    print('hello2451'.isdecimal()) # False
    print('123'.isdecimal()) # True
    # 判断是否是一个字母,且至少有一个字符
    print("hello".isalpha()) # True
    # 判断是否是个数字
    print('123'.isdigit()) # True
    # 判断是否是个数字
    print('9999'.isnumeric()) # True
    # 判断非法字符
    print('23-hl'.isidentifier()) # True
    # 判断字符串是否全是小写
    print('Abc'.islower())     # False
    # 判断字符串是否全是大写
    print('Abc'.isupper())     # False
    # 判断是否全是一个空格,只包含空格
    print(' hello'.isspace())  # False
    # 判断是否首字母大写
    print("Hello World".istitle())   # True
    print("Hello WOrld".istitle())   # False
    # 所有的大写变小写
    print("HELLoWorld".lower())      # helloworld
    # 所有的小写变大写
    print("helloworld".upper())      # HELLOWORLD
    # 大写变小写,小写变大写
    print('Hello WORLD'.swapcase())  # hELLO world
    # 剧中对齐,空格用*填充
    print('hello'.center(50, '*'))
    # 全部靠左对齐
    print("Hello world".ljust(50, '*'))
    # 全部靠右对齐
    print("Hello world".rjust(50, '*'))
    # 去除左右的空格和换行符
    print("   Hello World
       ".strip())  # Hello World
    # 去除左的空格和换行符
    print("   Hello World
       ".lstrip())  # Hello World
    # 去除右的空格和换行符
    print("   Hello World
       ".rstrip())  #   Hello World
    # 字符串的替换
    print("hello world".replace('llo', 'TTT')) # heTTT world
    print("hello world".replace('l', 'Z',  2)) # heZZo world 只替换2次,第三个l忽略
    # 按照title的格式输出
    print('hello world'.title()) # Hello World
  • 相关阅读:
    innerHTML与innerText区别
    HTML5中的数据集dataset和自定义属性data-*
    HTTP协议
    Javascript 中的 && 和 || 使用小结
    JavaScript 实现回文解码
    sublime中用less实现css预编译
    jQuery事件绑定的四种方法
    前端学习过程中需要看的书籍
    Echarts学习宝典
    Vue插槽
  • 原文地址:https://www.cnblogs.com/ftl1012/p/9382009.html
Copyright © 2011-2022 走看看