zoukankan      html  css  js  c++  java
  • 字符串类型-课堂笔记

    字符串类型

    一:基本使用
    1 用途: 描述性质的数据,比如人的名字,单个爱好,地址

    2 定义方式
    name='egon' #name=str('egon')
    x=str(1) # str能将任意数据类型强转为str类型。
    y=str(1.1)
    z=str([1,2,3])
    n=str({'a':1}) # 输出都是str
    print(type(x))
    print(type(y))
    print(type(z))
    print(type(n))

    3 常用操作+内置的方法
    优先掌握的操作(*****):
    1、按索引取值(正向取+反向取) :只能取,不能修改(包括删除,替换等操作)
    注意:空字符串也算是一个字符,也占一个索引位置。
    msg='hello world#'
    print(type(msg[-1])) # 字符串里任意一个字符类型都是str,包括空格和特殊符号
    print(msg[-1])
    msg[2]='A' # 会报错,字符串里值只能取,不能改。

    2、切片(顾头不顾尾,步长)
    msg='hello world'
    print(msg[1:5],type(msg[1:5])) # 输出是“ello” # str
    print(msg[6:-1])
    print(msg[6:11])
    print(msg[6:])
    print(msg[6::2])
    了解(**)
    步长为负的情况(翻转)
    msg='hello world'
    print(msg[0:])
    print(msg[::-1]) # 所有的值,倒过来。即 dlrow olleh,步长为1.
    print(type(msg[::-1])) # str类型
    msg='hello world'
    print(msg[-3:-6:-1])
    print(msg[6:9:-1]) # 没有返回值
    print(msg[:0:-1]) # dlrow olle
    print(msg[0::-1]) # h 重点:这个是从索引为0开始翻转,向左。
    print(msg[:1:-1]) # dlrow oll 默认的是从-1 开始。

    3、长度len
    空字符也算是长度
    msg='hello world'
    print(len(msg))

    4、成员运算in和not in
    print('SB' in 'my name is alex,alex is SB')
    print('alex' in 'my name is alex,alex is SB')
    print('egon' not in 'my name is alex,alex is SB') # 推荐,好理解
    print(not 'egon' in 'my name is alex,alex is SB')


    5、移除空白strip
    注意:移除是从左到右然后从右到左寻找需要移除的,碰到不需要移除的程序就会停下。
    name=' e gon '
    print(name.strip(' '))
    print(name.strip()) # 不输入默认为移除空白字符串
    name='****A*e*gon****'
    print(name.strip('*'))

    name='****egon****'
    print(name.lstrip('*'))
    print(name.rstrip('*'))
    pwd=input('>>: ').strip() #pwd='123 ' # 用户输入可能会有空格
    if pwd == '123':
    print('login successful')


    msg='cccabcdefgccccc'
    # 'c'
    print(msg.strip('c'))

    print('*-=egon *&^'.strip('-= *&^')) # ***

    6、切分split
    分开来取索引
    msg='egon:18:male:180:160'
    l=msg.split(':')
    print(l)
    print(l[3])

    7、循环
    msg='hello world'
    for x in msg:
    print(x)

    需要掌握的操作(****)
    1、strip,lstrip,rstrip
    2、lower,upper # lower 字符串里面的字符都变小
    print(["ADFDRE"].lower()) # 只有字符串可以用lower
    name='EoN'
    print(name.lower())

    name='egonN'
    print(name.upper())

    3、startswith,endswith # 判断字符串是否以输入的字符串开头,以输入的字符串(可以是一个短字符串,也可以是长的字符串)结尾
    print('alex is SB'.startswith('alex'))
    print('alex is SB'.endswith('B'))

    4、format的三种玩法 *****
    print('my name is %s my age is %s' %('egon',18))
    print('my name is {name} my age is {age}'.format(age=18,name='egon')) # 可以打破位置的限制,但仍能指名道姓地为指定的参数传值

    print('my name is {} my age is {}'.format('egon',18)) # 位置对应。
    print('my name is {0} my age is {1} {1} {1} {1}'.format('egon',18))

    5、split,rsplit
    info='egon:18:male'
    print(info.split(':',1)) # 后面可以设定分割的次数,不设定次数则分割所有
    print(info.split(":")) # 把所有符合条件的都分割。

    print(info.split(':',1)) #['egon','18:male']
    print(info.rsplit(':',1)) #['egon:18','male']

    6、join:只能将元素全为字符串的列表拼成一个大的字符串
    info='egon:18:male'
    l=info.split(':') # 以:为借口,分割字符串。
    print(l)
    new_info='common'.join(l) # 拼接字符串,这里是吧列表里的元素拼接,拼接出来的是字符串。
    print(new_info,type(new_info))

    num=['a','b','c']
    print(':'.join(num)) #'a'+':'+'b'+':'+'c' # 输出结果 a:b:c

    num=[1,2,'c']
    ':'.join(num) #1+':'+2+':'+'c' # 会报错,只能字符串之间进行拼接

    7、replace
    replace(old_str,new_str,替换的个数(不写默认为替换全部))
    msg='my name is wupeiqi,wupeiqi is SB'
    print(msg.replace('wupeiqi','Pig',1))
    print(msg.replace('wupeiqi','Pig'))
    print(msg)

    8、isdigit # 检查输入的字符串是否由纯数字组成
    print('111.1'.isdigit()) # False
    print('1111'.isdigit()) # True

    AGE=73
    age=input('>>: ').strip() #age='asdfasdf'
    if age.isdigit():
    age=int(age)
    if age > AGE:
    print('too big')
    elif age < AGE:
    print('too small')
    else:
    print('you got it')
    else:
    print('必须输入数字啊傻叉')

    其他操作(了解即可)(**)
    1、find,rfind,index,rindex,count
    index用法 msg.index(str,0,len(msg)) 可以用索引控制查找的范围
    msg = 'my name is alex,alex is hahaha'
    print(msg.find('alex')) # "alex"刚出现的索引 11
    print(msg.find('SB')) # 找不到会返回-1

    print(msg.index('alex'))
    print(msg.index('SB')) # 找不到index会报错 **** index和find区别 index找不到会报错,find找不到会返回-1.

    print(msg.find('alex',0,3))
    msg = 'my name is alex,alex is hahaha'
    print(msg.count('alex')) # 2 # 返回查找字符串的个数。
    print(msg.count('alex',0,15)) # 1

    2、center,ljust,rjust,zfill
    print('info egon'.center(20,'-')) # 中间是info egon,加上两边的-以及空格一共占20个位置。
    print('info egon'.ljust(50,'-')) # 左对齐
    print('info egon'.rjust(50,'-')) # 右对齐
    print('info egon'.zfill(50)) # zero fill 默认为右对齐,其他用0填满

    3、expandtabs #
    print('a b c'.expandtabs(10)) # a b c

    4、captalize,swapcase,title
    print('my name is egon'.capitalize()) # My name is egon # 一句话开头大写。
    print('my Name Is egon'.swapcase()) # MY nAME iS EGON # 反转,大写变小写,小写变成大写。
    print('my nAme is eGon'.title()) # My Name Is Egon # 首字母全部大写,除首字母以外全部小写。*****

    # 5、is数字系列
    num1 = b'4' # bytes
    num2 = u'4' # unicode,python3中无需加u就是unicode
    num3 = '壹' # 中文数字
    num4 = 'Ⅳ' # 罗马数字

    isdigit():bytes,unicode
    isdigit能够认出bytes和unicode
    print(num1.isdigit())
    print(num2.isdigit())
    print(num3.isdigit())
    print(num4.isdigit())

    # True
    True
    False
    False
    isdecimal():unicode
    print(num2.isdecimal())
    print(num3.isdecimal())
    print(num4.isdecimal())

    True
    False
    False

    isnumeric;unicode,中文,罗马(bytes不行)
    print(num2.isnumeric())
    print(num3.isnumeric())
    print(num4.isnumeric())

    6、is其他
    isalnum 和 isalpha 中间不能有空格
    print('abasdf 123123'.isalnum()) # 检查字符串是不是都有字母和数字组成,不能有空格 *****
    print('asdfas df'.isalpha()) # 检查字符串是不是都由字母组成,不能有空格
    print('egon'.islower()) # 检查字符串是否都由小写字母组成,可以有空格。
    print('ABC'.isupper())

    print(' '.isspace()) # 检查给出的字符串是否是由空字符串组成,必须有空字符串,才会显示True
    print('My Name Is Egon'.istitle()) # 检查首字母是否大写,除首字母其他字母都小写。*****


    二:该类型总结
    1 存一个值or存多个值
    只能存一个值

    2 有序or无序
    有序

    3 可变or不可变
    不可变

    name='egon'
    print(id(name))
    name='alex'
    print(id(name))

  • 相关阅读:
    jmeter接口测试 Base64加密(函数助手添加自定义函数)
    jmeter接口测试 上传文件(multipart/formdata数据请求)
    python入门_模块2
    python_元类
    python day03_ 文件处理
    Python入门day04_函数与装饰器
    python_字符编码
    三元表达式、列表推导式、生成器表达式、递归、匿名函数、内置函数
    JavaScriptproperty
    我在博客园的第一个博客
  • 原文地址:https://www.cnblogs.com/Roc-Atlantis/p/9112826.html
Copyright © 2011-2022 走看看