python基础语法,字符串

(一)字符串定义

字面常量,不可变

  • 'string’join(spl)
    将可迭代对象连接起来,使用string作为分隔符
    可迭代对象本身都是字符串
    返回一个新的字符串
a = 'abcdef'
string = ','.join(a)

返回结果

‘a,b,c,d,e,f’

  • ’ '.split( ,切分割的最大数)
    返回一个列表,默认是空白字符来切
string.split(',')   

返回结果

[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]

  • ’ '.partition('分隔符’)
    返回一个元组,分隔成两段,分割符也保留
string.partition(',')  

返回结果

(‘a’, ‘,’, ‘b,c,d,e,f’)

(二)字符串大小写

  • upper()
    全大写
string.upper()

返回结果

‘A,B,C,D,E,F’

  • lower()
    全小写
  • string.upper()
            string.lower()

返回结果

‘a,b,c,d,e,f’

大小写,做判断的时候使用

  • swapcase()
    交换大小写
    中文处理一般没用

(三)字符串的排版

  • title() ->str
    标题每个单词都大写
string.title()  

返回结果

‘A,B,C,D,E,F’ #英文报纸题目格式

  • capitalize() ->
    首字母大写
string.capitalize()

返回结果

‘A,b,c,d,e,f’

  • center(windth[,filchar]) ->str
    width,打印宽度
    fillchar 填充的字符
            string.center(30,'$')

返回结果

‘$$$$$$$$$a,b,c,d,e,f$$$$$$$$$$’

  • zfill -> str
  • width打印宽度,居右,左边用0填充
  • lfill(width[,fillchar]) ->str 左对齐
  • rjust(width[,fillchar]) ->str 右对齐

(四)字符串修改

  • str.replace(‘被替换的字符串’,‘要替换的字符串’,‘替换的次数’)
        string.replace('a,b,c,d,e,f','A,B,C,D,E,F')

返回结果

‘A,B,C,D,E,F’

  • str.strip([chars])
    返回一个新的字符串,从字符串两端去除指定的字符集chars中的所有字符
    如果chars没有指定,去除两端的空白字符
            string.strip('A,F')

返回结果

‘B,C,D,E’

  • find(sub[,start[,end]]) ->int
    在指定区间内[start,end],从左至右。查找子串sub,查找到返回索引,找不到返回-1
            string.find(a)

返回索引:

0

  • rfind(sub[,start[,end]]) ->int
    从右侧开始查找
  • index(sub[,start,[,end]]) ->int
    在指定区间内[start,end],从左至右。查找子串sub,查找到返回索引,找不到返回ValueError
  • rindex(sub[,start[,end]]) ->返回索引,或者ValueError
  • count(sub)
    在指定区间[start,end],从左至右,统计子串sub出现的次数
string.count(',')

返回结果

5

  • endswith(suffix[,start[end]]) ->bool
    在指定的区间[start,end],字符串是否以suffix结尾
string.endswith('f')

返回结果

True

  • startwith(prefix[start,[,end]]) ->
    在指定区间[start,end],字符串是否以suffix开头
string.startswith('a')

返回结果

True

(五)format函数格式字符串语法------Python鼓励使用

  • ”{}{xxx}".format(*args,**kwargs) ->str
    args是位置参数,是一个元组
    kwargs是关键字参数,是一个字典
    花括号表示占位符
    {}表示按顺序匹配位置参数,{n}表示取位置参数索引为n的值
    {xxx}表示在关键字参数中搜索名称一致
    {{}}表示打印花括号
  • 位置参数
    “{}:{}”.format(‘192.168.1.100’,‘8888’),这就是按照位置顺序用位置参数替换前面的格式字符串的占位符中
    返回结果:
    '192.168.1.100:8888'
  • 关键字参数或命名参数
    “{server}{1}:{0}”.format(8888,‘192.168.1.100’,server=‘Web Server Info:’),位置参数按照序号匹配,关键字参照按照名词匹配

‘Web Server Info:192.168.1.100:8888’

  • 访问元素 ->可以根据索引访问元组,列表,字符串的值
    "{0[0]},{0[1]}".format(('baidu','com'))

返回结果

‘baidu,com’

  • 对象属性访问
    from collections import namedtuple
    point = namedtuple('point','x y')
    p = point(4,5)
    "{{{0,x},{0.y}}}".format(p)
    '{4,5}'
  • 对齐
    '{0}*{1}={2:<2}'.format(3,2,2*3)  

返回结果

'3*2=6 ’

    '{0}*{1}={2:<02}'.format(3,2,2*3)  

返回结果

‘3*2=60’

    '{0}*{1}={2:>02}'.format(3,2,2*3)       #向右补齐,不够左边补0

返回结果

‘3*2=06’

    '{:^30}'.format('centered')  

返回结果

’              centered              ’

    '{:*^30}'.format('centered')

返回结果

‘*********centered***********’

  • 进制
"int:{0:d};hex{0:x};oct:{0:o};bin:{0:b}".format(42)

返回结果

‘int:42;hex2a;oct:52;bin:101010’

"int:{0:d};hex:{0:#x};oct:{0:#o};bin:{0:#b}".format(42)

返回结果

‘int:42;hex:0x2a;oct:0o52;bin:0b101010’

octets = [192,168,0,1]
    '{:02X}{:02X}{:02X}{:02X}'.format(*octets)

返回结果

‘C0A80001’

(六)浮点数:

    print("{}".format(3**0.5))      #3的开方数  
    1.7320508075688772
    print("{:f}".format(3**0.5))    #1.732051#返回默认的6位数
    1.732051
    rint(“{:>f}".format(3**0.5))     #右对齐
    1.732051
    print("{:2}".format(102.231))   #不影响数字宽度为2,当小数点后个数比2大,返回默认的6位
    1.732051
    print("{:.2}".format(3**0.5))   #精确到两个数字
    1.7
    print("{:.2f}").format(3**0.5)) #精确到小数点后两位
    1.73
    print("{:3.2f}".format(3**0.5)) #宽度为三,小数点后两位
    1.73
    print("{:3.3f}".format(0.275))  #0.275 宽度为三,小数点后三位
    0.275
    print("{:3.3%}".format(1/3))     #按百分比来换算
    33.333%
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值