zoukankan      html  css  js  c++  java
  • 字符串

    字符串是一个有序的字符的集合,用于存储和表示基本的文本信息。

    特性:

    有序,不可变

    id (a)   #查看内存地址

    >>> s= 'Hello World!'
    >>> print(s.swapcase())  #大写变小写,小写变大写
    hELLO wORLD!

    >>> print(s.capitalize())  #第一个字母大写,其它字母小写
    Hello world!

    >>> s.casefold()    #把所有字母都小写
    'hello world!'

    >>> s.center(50,'*')  #输入长度和想要打印的字符,输出
    '*******************Hello World!*******************'
    >>> s.center(50,'-')
    '-------------------Hello World!-------------------'

    >>> s.count('o')    #统计s 中有几个'o'
    2
    >>> s.count('o',0,5)   #统计s 中0-5的索引位有几个'o' 
    0

    >>> s.endswith('!')    #查询s是否以'!'结尾,返回布尔值
    True
    >>> s.endswith('!sdf')
    False

    >>> s.expandtabs()    #扩展tab键长度变长
    'Hello World!'

    >>> s2= 'a b'
    >>> print(s2)
    a b
    >>> s2.expandtabs(20)  #扩展tab键长度20位长度
    'a     b'

    >>> s.find('o')      #查找 ‘o’,返回索引
    4
    >>> s.find('osdf')       #查找'osdf',如果没有返回负数
    -1
    >>> s.find('o',0,3)    #查找'o'.索引0-3的位置找
    -1

    >>> s3 = 'my names is {0}, i an {1} years old'   #{0},{1}等着下面.format 传值
    >>> s3
    'my names is {0}, i an {1} years old'  
    >>> s3.format('Alex',22)          # format 传值
    'my names is Alex, i an 22 years old'

    >>> s.index('o')        #返回'o'的索引
    4

    >>> '22'.isalpha()    #判断是不是字符
    False
    >>> '22'.isalnum()    #判断是不是数字
    True
    >>> '22!'.isalnum()
    False
    >>> 'dd'.isalpha()
    True
    >>> '33'.isdecimal()    #判断是不是整数
    True
    >>> '333'.isdigit()    #判断是不是整数
    True

    >>> '333'.isidentifier()
    False
    >>> 'd333'.isidentifier()  #判断是不是一个合法的变量
    True

    >>> 'd333'.islower()  #判断是不是小写
    True

    >>> s.istitle()    #判断是不是一个标题格式(首字母大写是标题)
    False

    >>> ''.join(names)  #
    'alexjackrain'
    >>> ','.join(names)  #把列表拼接成字符串,以会什么来连接区分
    'alex,jack,rain'

    >>> s.ljust(50)
    'Hello world! '
    >>> s.ljust(50,'-')  把字符串从左边开始变成长度为50的字符串,可填充符号。
    'Hello world!--------------------------------------'

    >>> len(s.ljust(50,'#'))  #返回字符串长度。
    50

    >>> s.lower()    #字符串变小写
    'hello world!'
    >>> s.upper()    #字符串变大写
    'HELLO WORLD!'

    >>> s
    ' hello world'
    >>> s.strip()    #脱掉换行,和空格。
    'hello world'

    >>> s.lstrip()    #脱掉换行,和空格。只脱左边
    'hello world'
    >>> s.rstrip()    #脱掉换行,和空格。只脱右边
    ' hello world'

    >>> str_in = 'abcdef'      #制作密码表密文
    >>> str_out = '!@#$%^'   # 制作密码表明文被解释
    >>> str.maketrans(str_in,str_out)  #制作密码表语法
    {97: 33, 98: 64, 99: 35, 100: 36, 101: 37, 102: 94}
    >>> table=str.maketrans(str_in,str_out)   #密码表赋值  
    >>> table
    {97: 33, 98: 64, 99: 35, 100: 36, 101: 37, 102: 94}
    >>> s
    ' hello world'
    >>> s.translate(table)      #密码表解密
    ' h%llo worl$'

    >>> s.partition('o')    #以‘o’为中心,把字符串分成两半
    (' hell', 'o', ' world')
    >>> s.replace('h','H')      #替换
    ' Hello world'
    >>> s.replace('o','-',1)    #替换 'o '为‘-’,只替换一次
    ' hell- world'

    >>> s.split()    #把字符串分成列表
    ['hello', 'world']
    >>> s.split('o')  #把字符串分成列表,以‘o’分,o会删掉。  
    ['hell', ' w', 'rld']

    >>> s = 'a b alex c'
    >>> s.splitlines()  #把字符串 按行 分成列表,以‘o’分,o会删掉
    ['a', 'b', 'alex', 'c']

    >>> s= 'hello world!'
    >>> s.startswith('he')  #判断字符串以‘he’开头
    True
    >>> s.endswith('world')  #判断字符串以‘world’结尾
    False

    >>> s.swapcase()    #把字符串换成大写
    'HELLO WORLD!'

    isdigit   replace find count  index  strip  center split format   join(这些方法必须要学会)

  • 相关阅读:
    Object有哪些方法?
    去除掉myeclipse2017页面右上角的图片
    报错 IllegalArgumentException occurred calling getter of cn.itcast.entity.Customer.cid
    如果在applicationContext.xml中没有配置bean的属性,那么也会导致空指针异常
    报错HTTP Status 500
    srm开发(基于ssh)(4)
    报错HTTP Status 500
    HTTP Status 500
    数字
    算法总结——树状数组
  • 原文地址:https://www.cnblogs.com/anzhangjun/p/8361801.html
Copyright © 2011-2022 走看看