zoukankan      html  css  js  c++  java
  • 字符串操作、数据类型转换


    字符串主要操作方法:

    st = 'hello kitty'
    print(st.count('l'))        #统计元素出现次数
    print(st.center(50,'#'))    # 居中
    print(st.startswith('he'))   # 判断是否以某个字符串开头
    print(st.find('t'))       #返回索引值
    print(st.format(name='alex',age=37))      # 格式化输出的另一种方式 
    print('My tLtle'.lower())        #转换成小写
    print('My tLtle'.upper())        #转换成大写
    print(' My tLtle '.strip())        #左右空格及制表符、换行符删除
    print('My title title'.replace('itle','lesson',1))  #整体替换、可指定位置字符串替换
    print('My title title'.split('i',1))        #字符串用,分割,用指定字符串替换,分隔

    数据类型转换:

    1,int <——> str

    s = str(int),没有条件

    i = int(str),转换的字符串必须是数字

    2,list <——> tuble

    list = list(tuple)

    tuple = tuple(list)

    3,list <——> set

    list = list(set)

    set = set(list)

    4,tuple <——> set

    tuple = tuple(set)

    set = set(tuple)

    5,重要转换,

    列表转字符串:  list ——> str

    lst = ["1","2","3"]
    print("".join(lst)) #123

    6,字符串转列表:  str ——> list

    s = "host1 host2 host3"
    print(s.split())#[host1,host2,host3]

     

  • 相关阅读:
    python学习笔记二--列表
    python学习笔记一--字符串
    写点什么呢
    nagios&pnp4nagios--yum 安装
    敏捷开发的思路
    Foreman--管理PuppetClient
    url编码解码的问题(urlencode/quote)
    json数据的处理和转化(loads/load/dump/dumps)
    http和https的区别
    python中requests的用法总结
  • 原文地址:https://www.cnblogs.com/zzzhao/p/11354813.html
Copyright © 2011-2022 走看看