zoukankan      html  css  js  c++  java
  • 一步一步学python(三)

    1、基本字符串操作

    序列和元组的索引、分片、乘法、判断成员资格、求长度、取最小值和最大值对字符串同样适用。

    字符串是不可变的

    2、字符串格式化

    %左侧放字符串右侧放格式化的值。一般情况下使用元组

    format = "hello %s"

    str = ('world')

    print format % values

    %s称作转换说明符。用来标记插入的位置

    如果要在字符串中输出%必须使用%%

    格式化实数%f

    3、简单转换

    ‘price of apple:$%d’ % 10

    * 可以代表字段宽和精度,数值从参数中读取

    4、字符串方法

    find 查找字符串如果没有找到返回-1

    >>‘with a moo-moo here,and a moo-moo there’.find('moo')

    >>7

    join 在队列中添加元素

    >>>seq = [1,2,3,4,5]

    >>>sep = '+'

    >>>sep.join(seq)

    会报错,要连接的必须都是字符串才行

    lower 返回字符串的小写字母版

    >>>' Trondheim Hammer Dance ' . lower()

    >>> ' trondheim hammer dance'

    titile 所有单词的首字母大写、其他字母小写

    capwords所有单词的首字母大写、其他字母小写

    replace 返回某字符串的所有匹配项均被替换之后的字符串

    >>>' this is a test'.replace(' is ' , ' eez ' )

    >>>'theez eez a test'

    split 分割字符串

    >>' 1+2+3+4 ' . split('+')

    >>[ '1', '2', '3', '4',]

    如果不提供任何分割符 默认将使用空格

    strip 去除两侧的空格

    maketrans

  • 相关阅读:
    解决asp.net mvc的跨域请求问题
    centos安装nodejs
    webapi中配置返回的时间数据格式
    centos安装django
    在Linux CentOS 6.6上安装Python 2.7.9
    nginx日志切割脚本
    apache单ip配置多端口多站点
    centos开启rewrite功能
    Fast Matrix Operations
    1400
  • 原文地址:https://www.cnblogs.com/chentongxin/p/3483221.html
Copyright © 2011-2022 走看看