zoukankan      html  css  js  c++  java
  • 1.1数据类型 (string)

    字符串创建

    创建字符串,只需给变量分配一个值即可。例如:

    first_str = “hello world!!!”

    second_str = “hello python!!!”

    字符串访问

    访问子字符串,可以用方括号来截取字符串。例如:

    first_str = "hello world!!!"
    print "first_str[0]:",first_str[0]
    first_str[0]: h
    print "first_str[0:4]:%s"%(first_str[0:4])

    first_str[0:4]:hell   #注意截取结束边界不算在截取范围内

    字符串更新

    对已存在的字符串修改,赋值给另一个变量。例如:

    first_str = "hello world!!!"
    update_str = first_str[:7] + "python!!!"
    print "update_st:%s"%(update_str)
    update_str:hello wpython!!!

    字符串运算符: +、*、[]、[:]、in、not in 、r/R

    a = “hello”,b = "python"

    操作符 描述 实例
    + 字符串连接

     >>> a  + b

    "hello python"

    * 字符串重复

     >>>a * 2

    "hellohello"

    [] 通过索引获取字符串中的字符

     >>>a[1]

    "e"

    [:] 截取字符串中的一部分

     >>>a[1:4]

    "ell"

    in 成员运算符,如果字符串中包含给定的字符返回True

     >>>"h" in a

    True

    not in 成员运算符,如果字符串中不含给定字符返回True

     >>>"m" not in a

    True

     r/R  原始字符

     >>>print r" "

    % 格式化字符串 print "a:%s"%(a)

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    字符串格式化

    1、%实现,例如:

    >>> print "My name is %s and weight is %d kg"%("luyangchun",48.6)
    My name is luyangchun and weight is 48 kg

    2、format实现

    >>> print "My name is {0} and weight is {1} kg".format("luyangchun",48.6)
    My name is luyangchun and weight is 48.6 kg
    字符串内建函数

    1、split():通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串

    语法:string.split(str = "",num = string.count(str))

    参数:stri--分隔符切片String,num--分割次数,有指定,则分割num个子字符串 

    返回:返回切割后的字符串列表

    例子:

    >>> split_str = "hello world,hello python,hello yangyang"
    >>> split_list = split_str.split(",")
    >>> split_list
    ['hello world', 'hello python', 'hello yangyang']
    >>> split_num_list = split_str.split(",",1)
    >>> split_num_list
    ['hello world', 'hello python,hello yangyang']

    2、jion():用于将序列中的元素用指定的字符链接生成一个新的字符串

    jion()语法:str.join(sequence)

    参数:seauence--要连接的元素序列

    返回值:通过指定字符连接序列中元素后生成的新的字符串

    例子:

    >>> str_list = ["hello world","hello python","hello yangyang"]
    >>> join_str = ",".join(str_list)
    >>> join_str
    'hello world,hello python,hello yangyang'

    3、replace():把字符串中old str 替换成 new str,指定参数max,则替换不超过max次

    语法:str.replace(old,new[,max])

    参数:old--被替换的字符串,new--新字符串,max--可选字符串,替换不超过max次

    返回值:返回old替换成new后生成的新的字符串

    例子:

    >>> old_str = "hello world,hello python,hello yangyang"
    >>> new_str = old_str.replace("hello","love")
    >>> new_str
    'love world,love python,love yangyang'
    >>> new_max_str = old_str.replace("hello","love",2)
    >>> new_max_str
    'love world,love python,hello yangyang'

    4、find():检测字符串中是否包含子字符串str,若指定开始和结束范围,则检查是否包含在指定范围内,若包含返回子字符串的开始的索引值,否则返回-1

    语法:string.find(str,beg,end)

    参数:str--检索字符串,beg--开始索引,默认为0,end--结束索引,默认为字符串长度

    返回:如果包含子字符串返回开始的索引值,否则返回-1

    例子:

    >>> find_str = "hello world,hello python,hello yangyang"
    >>> str_index = find_str.find("hello")
    >>> str_index
    0
    >>> str_index1 = find_str.find("hello",0,len(find_str))
    >>> str_index1
    0

     

     

     

  • 相关阅读:
    php memcache分布式和要注意的问题
    PHP延迟静态绑定(本文属于转发)
    WebSocket实战
    HTML5本地存储(Local Storage) 的前世今生
    HTML5本地存储——IndexedDB
    HTML5 FileReader
    HTML5 FormData对象
    2017-2018-1 20155225 实验四 外设驱动程序设计
    2017-2018-1 20155225 《信息安全系统设计基础》第十一周学习总结
    Linux下的IPC机制
  • 原文地址:https://www.cnblogs.com/yangyangchunchun/p/7262323.html
Copyright © 2011-2022 走看看