• 内置方法练习(一)


    字符串内置方法练习

    # 字符串长度
    name = 'his name is tom'
    print(len(name))
    
    15
    
    #字符串索引取值
    print(name[6])
    
    m
    
    #字符串切片练习
    print(name[1:7:3]) # 顾头不顾尾,步长取的值也包括在步长内 
    print(name[::-1]) # 字符串翻转
    print(name[:-1]) # 取最后一个值前面所有值
    print(name[1:]) # 取第一个值后面所有值
    print(name[-3:]) # 逆序取值
    
    ia
    motsiemansih
    hisnameisto
    isnameistom
    tom
    
    # 字符串成员运算
    print('tom' in name) # tom在name中
    print('tom' not in name) # tom不在name中
    print(name)
    
    True
    False
    his name is tom
    
    # 字符串移除空白(只能移除左右空白)
    name = '*** his name is jack  ***'
    print(name.strip())  # 默认移除空格 空格优先级最高 同等情况下 添加的参数优先级不如空格 可以添加参数 如下
    print(name.strip('*'))  # 切除两边指定的数值
    print(name.lstrip('*')) # 切除左边指定的数
    print(name.rstrip('*')) # 切除右边值定的数
    
    *** his name is jack  ***
     his name is jack  
     his name is jack  ***
    *** his name is jack  
    
    # 字符串切分
    name_list = 'tom,jack,ellie'
    print(name_list.split(',')) # 切分指定的参数成为列表
    print(name_list.split(',',1)) # 切分指定的几个数成为列表
    print(name.rsplit(' ',1))  # 切分字符串 从右边开始
    
    ['tom', 'jack', 'ellie']
    ['tom', 'jack,ellie']
    ['His name is', 'Tom']
    
    # 字符串循环
    name = 'his name is tom'
    for i in name:
        print(i)
    
    h
    i
    s
     
    n
    a
    m
    e
     
    i
    s
     
    t
    o
    m
    
    # 字符串大小写转换
    name = 'His name is Tom,His name is Tom'
    print(name.lower())  # 字符串全部转化为小写
    print(name.upper()) # 字符串全部转化为大写
    
    his name is tom,his name is tom
    HIS NAME IS TOM,HIS NAME IS TOM
    
    # 判断字符串是以什么开头或者以什么结尾
    print(name.startswith('His')) # 判断字符串是否以His开头
    print(name.endswith('tom')) # 判断字符串是否以tom结尾
    
    True
    False
    
    # 字符串的替换
    print(name.replace('Tom','Jack')) # 替换默认为全部替换,可以添加参数 参数为替换的次数 如下
    print(name.replace('Tom','Jack',1))
    
    His name is Jack,His name is Jack
    His name is Jack,His name is Tom
    
    # 以指定的字符分割列表
    info_list = ['tom','18','sex']
    print(':'.join(info_list))
    
    tom:18:sex
    
    # 判断字符串是否为整数
    number = '123'
    number_f = '123.7'
    print(number.isdigit())
    print(number_f.isdigit())
    
    True
    False
    

    列表内置方法练习

    name_list = ['tom','jack','ellie','chris']
    # 索引取值
    print(name_list[3])
    
    chris
    
    # 切片
    print(name_list[0:])
    print(name_list[::-1])
    print(name_list[0:3:2])
    
    ['tom', 'jack', 'ellie', 'chris']
    ['chris', 'ellie', 'jack', 'tom']
    ['tom', 'ellie']
    
    # len
    print(len(name_list))
    
    4
    
    # 成员运算
    print('ellie' in name_list)
    print('ellie'not in name_list)
    
    True
    False
    
    #追加值
    name_list.append('kuck')
    print(name_list)
    
    ['ellie', 'chris', 'kuck', 'kuck']
    
    # 删除 
    del name_list[3]
    print(name_list)
    
    ['tom', 'jack', 'ellie']
    
    # 循环
    for i in name_list:
        print(i)
    
    tom
    jack
    ellie
    
    # 插入
    name_list.insert(0,'Alan')  # 0表示要插入的位置
    print(name_list)
    
    ['Alan', 'tom', 'chris', 'jack', 'ellie']
    
    # 移除
    # name_list.pop(0) # 默认移除最后一个元素 索引移除
    name_list.remove('ellie')  # 直接移除元素 value
    print(name_list)
    
    ['tom', 'jack']
    
    # count统计元素出现的次数
    print(name_list.count('jack'))
    print(name_list)
    
    3
    ['tom', 'jack', 'Alan', 'jack', 'chris', 'kuck', 'jack']
    
    # 获取索引值
    print(name_list.index('tom'))
    
    0
    
    #清除列表
    hobby = ['sing','dance','ran']
    print(hobby.clear())
    
    None
    
    #列表复制
    name_list_c = name_list.copy()
    print(name_list_c)
    
    ['tom', 'jack', 'Alan', 'jack', 'chris', 'kuck', 'jack']
    
    #列表拼接
    name_list.extend(name_list_c)
    print(name_list)
    
    ['tom', 'jack', 'Alan', 'jack', 'chris', 'kuck', 'jack', 'tom', 'jack', 'Alan', 'jack', 'chris', 'kuck', 'jack']
    
    #列表反转
    print(name_list.reverse())
    
    None
    
    # 排序
    name_list = ['tom','jack','ellie']
    print(name_list.sort())
    
    None
    
    
    
  • 相关阅读:
    html+css设计简单的博客首页
    js基础知识总结(一)
    css基础知识总结
    HTML基础知识
    链表常见题目--附具体分析和代码
    程序的内存分配
    python线程互斥锁递归锁死锁
    动态规划算法python实现
    ECMAScript 6 开篇准备
    弦生成器
  • 原文地址:https://www.cnblogs.com/kuck/p/11279076.html
Copyright © 2020-2023  润新知