美文网首页
python数据类型

python数据类型

作者: 深吸一口气 | 来源:发表于2018-06-24 00:26 被阅读0次

    1、字符串

    str = "nihaoshijie"
    # 字符串转化成列表
    list1 = list(str)
    print(list1)
    >>>["n","i","h","a","o","s","h","i","j","i","e"]
    list2 = str.split("i")
    print(list2)
    >>>["n","haosh","j","e"]
    
    #替换
    newStr = str.replace("i", "n")
    print(newStr)
    >>>"nnhaoshnjne"
    

    2、列表

    list = ["ni","hao","shi","jie"]
    # 列表转字符串
    str1 = "".join(list)
    print(str1)
    >>>"nihaoshijie"
    str2 = " ".join(list)
    print(str2)
    >>>"ni hao shi jie"
    str3 = "."join(list)
    print(str3)
    >>>"ni.hao.shi.jie"
    
    # 获取列表元素
    first = list[0]
    print(first)
    >>>"ni"
    second = list[1]
    print(second)
    >>>"hao"
    
    # 列表转元组
    tuple = tuple(list)
    print(tuple)
    >>>("ni","hao","shi","jie")
    

    3、字典

    dict = {"key1":"value1","key2":"value2","key3":"value3"}
    # 根据key取value
    value1 = dict.get("key1")
    

    相关文章

      网友评论

          本文标题:python数据类型

          本文链接:https://www.haomeiwen.com/subject/nlxjyftx.html