zoukankan      html  css  js  c++  java
  • python数据类型:int、str及其方法

    1.判断某个东西是否在某个东西里包含:in和not in。结果实际上是布尔值(true或false)

     eg:name = "王思骐"
    "王思骐" 字符串里面有三个字符,其中思骐称为子字符串/子序列
    整体注释:ctrl + ?

    name = "王思骐"
     if "思骐" in name:
         print('OK')
     else:
         print('Error')

    执行结果为OK

    name = "王思骐"
     if "王骐" in name:
         print('OK')
     else:
         print('Error')

    执行结果为Error。字符串中的字符必须相临才可以称为子序列,包含在其中

     2.运算符

    结果是值:
      算数运算:a = 10 * 10
      赋值运算:a += 1(等同于a = a + 1);a *= 1(等同于a = a * 1);a //=1(等同于a = a // 1)

    结果是布尔值:
      比较运算:a = 1 > 5
      逻辑运算:a = 1>6 or 1==1
      成员运算:a = "骐" in "王思骐"

    结果是布尔值的运算规则:从前到后依次计算,有括号先算括号里的内容

    执行结果:True or ==>真;True and ==>继续判断;False and ==>假;False or ==>继续判断

    3.数字类型的功能:int ,所有的功能都放在int里

    (1)将字符串转换为数字- int

    a = "123"
    print(type(a),a) 
    b = int(a)
    print(type(b),b)

    其中,type(a)显示数据类型

    num = "0011" 
    v = int(num, base=16) 
    print(v)

    其中,base=16表示按照16进制将字符串转换成10进制的数字,默认是按10进制进行转换

     (2)当前数字的二进制,至少用n位表示- bit_lenght

    a=5
    r = age.bit_length()
    print(r)

    4.字符串:str
    (1)首字母大写

    test = "aLex"
    v = test.capitalize()
    print(v)

    (2)所有变小写,其中casefold更牛逼,很多未知的对相应变小写

    test = "aLex"
    v1 = test.casefold()
    print(v1)
    v2 = test.lower()
    print(v2)

    (3)设置宽度,并将内容居中

    test = "aLex"
    v = test.center(20,*)
    print(v)

    其中,20 代指总长度;*是 空白位置填充,一个字符,可有可无

    (4)去字符串中寻找,寻找子序列的出现次数

    test = "aLexalexr"
    v = test.count('ex')
    print(v)
    test = "aLexalexr"
    v = test.count('ex',5,6) 
    print(v)

    其中,5,6是指从第五位开始,第六位结束

    (5)判断以什么什么结尾,判断以什么什么开始,输出结果均为true或false

    test = "alex"
    v = test.endswith('ex')
    v = test.startswith('ex')
    print(v)

    (6)从开始往后找,找到第一个之后,获取其位置,是从0开始数位置的

    test = "alexalex"
    v = test.find('ex')
    print(v)

    设定寻找的位置参数时,寻找的范围是左闭右开,例如v = test.find('ex',5,8)第五位找到第7位;未找到时间显示 -1

    test = "alexalex"
    v = test.index('8')
    print(v)

     index找不到,报错==》忽略掉,不好用

    (7)格式化,将一个字符串中的占位符替换为指定的值

    test = 'i am {name}, age {a}'
    print(test)
    v = test.format(name='alex',a=19)
    print(v)
    test = 'i am {0}, age {1}'
    print(test)
    v = test.format('alex',19)
    print(v)

    当占位符是数字时可直接按顺序替换

    (8)格式化,传入的值 {"name": 'alex', "a": 19}

    test = 'i am {name}, age {a}'
    v1 = test.format(name='df',a=10)
    v2 = test.format_map({"name":'alex', "a":19})

    使用format_map替换时需要注意传入的格式

    (9) 判断字符串中是否只包含字母和数字

    test = "123"
    v = test.isalnum()
    print(v)

     (10)制表符

    test = "username	email	password
    wangqiang1	ying@q.com	123
    wangqiang2	ying@q.com	123
    wangqiang3	ying@q.com	123"
    v = test.expandtabs(20)
    print(v)

    expandtabs表示断句,20字符为一组, 将前面未满一组的自动填充到20个字符。 表示换行

    (11)是否是字母,汉字

    test = "as2df"
    v = test.isalpha()
    print(v)

    (12)当前输入是否是数字

    test = "" 
    v1 = test.isdecimal()
    v2 = test.isdigit()
    v3 = test.isnumeric()
    print(v1,v2,v3)

    其中,test = "二"或者1或②

    isdecimal()最常用的;isdigit()可以识别②这种数字;test.isnumeric()可以识别②、二这种数字即所有数字

    (13)是否存在不可显示的字符

    test = "oiuas	dfkj"
    v = test.isprintable()
    print(v)

    制表符; 换行
    (14)判断是否全部是空格

    test = ""
    v = test.isspace()
    print(v)

    (15)判断是否是标题

    test = "Return True if all cased characters in S are uppercase and there is"
    v1 = test.istitle()
    print(v1)
    v2 = test.title()
    print(v2)
    v3 = v2.istitle()
    print(v3)

    istitle()判断是否是标题(标题单词的首字母均是大写);title()将内容转换成标题,即每个单词的首字母都换成大写

    (16)将字符串中的每一个元素按照指定分隔符进行拼接

    test = "你是风儿我是沙"
    print(test)
    v = "_".join(test)
    print(v)

    (17)判断是否全部是大小写 和 转换为大小写

    test = "Alex"
    v1 = test.islower()
    v2 = test.lower()
    print(v1, v2)
    v1 = test.isupper()
    v2 = test.upper()
    print(v1,v2)

    isupper()、islower()判断是否全是小写、大写;upper()、lower()将内容全部转换成大写、小写

    (18)移除指定字符串

    test = "ssddxadsddvdvxavfdgfg"
    v1 = test.lstrip('xa')
    v2 = test.rstrip('9lexxexa')
    v 3= test.strip('xa')
    print(v1,v2,v3)

    匹配字符串时,有限最多匹配。lstrip()匹配左面移除;rstrip()匹配右面移除;strip()匹配所有移除

    同时,还可以移除空白test.lstrip();test.rstrip();test.strip()。当出现 和 时也会直接移除

    (19)对应关系替换

    test = "aeiou"
    test1 = "12345"
    v = "asidufkasd;fiuadkf;adfkjalsdjf"
    m = str.maketrans("aeiou", "12345")
    new_v = v.translate(m)
    print(new_v)

    aeiou与12345一一对应

    (20)分割为三部分

    test = "testasdsddfg"
    v = test.partition('s')
    print(v)

    以第一个s为分割位;rpartition()是从右面寻找第一个s为分割位,显示分割位

    (21) 分割为指定个数

    test = "testasdsddfg"
    v = test.split('s',2)
    print(v)

    以前两个s为分割位;test.rsplit('s',2)从右面寻找前两个s为分割位,且不显示分割位

    (22) 分割,只能根据换行符分割。true,false:是否保留换行符

    test = "asdfadfasdf
    asdfasdf
    adfasdf"
    v = test.splitlines(False)
    print(v)

    (23) 判断字符串是否以xx开头/结尾

    test = "backend 1.1.1.1"
    v = test.startswith('a')
    print(v)

    startswith('a')判断字符串是否以a开头;endswith('a')判断字符串是否以a结尾

    (24)大小写相互转换

    test = "aLex"
    v = test.swapcase()
    print(v)

    (25) 判断是否是标识符(字母,数字,下划线 )

    a = "def"
    v = a.isidentifier()
    print(v)

    (26) 将指定字符串替换为指定字符串

    test = "alexalexalex"
    v1= test.replace("ex",'bbb')
    print(v1)
    v2= test.replace("ex",'bbb',2)
    print(v2)

    输出结果为:albbbalbbbalbbb;albbbalbbbalex即2表示"ex"只替换前两次

    5.str类型里比较重要的功能有:

    (1) join # '_'.join("asdfasdf"):将字符串中的每一个元素按照指定分隔符进行拼接

    test = "你是风儿我是沙"
    v = "_".join(test)
    print(v)

    输出结果为:你_是_风_儿_我_是_沙

    v = '_'.join("asdfasdf")
    print(v)

    输出结果为:a_s_d_f_a_s_d_f

    (2) split:按照指定分割位进行分割

    test = "testasdsddfg"
    v = test.split('s',2)
    print(v)

    输出结果为:['te', 'ta', 'dsddfg']

    以前两个s为分割位;test.rsplit('s',2)从右面寻找前两个s为分割位,且不显示分割位
    (3) find:从开始往后找,找到第一个之后,获取其位置

    test = "alexalex"
    v = test.find('ex')
    print(v)

    输出结果为:2

    从开始往后找,找到第一个之后,获取其位置,是从0开始数位置的
    (4) strip:移除指定字符串

    test = " ssddx "
    v1 = test.lstrip()
    v2 = test.rstrip()
    v3= test.strip()
    print(v1)
    print(v2)
    print(v3)

    输出结果为:

    ssddx                            #去掉左边空格,右边仍有
     ssddx                          #去掉右边空格,左边仍有
    ssddx                           #去掉两边空格

    strip还可以去除 、 。同时也可以去除指定字符串

    test = "xaexlex"
    v1 = test.lstrip('xa')
    v2 = test.rstrip('9lexxex')
    v3= test.strip('ex')
    print(v1)
    print(v2)
    print(v3)

    输出结果为:exlex;   xa;     aexl

    匹配字符串时,有限最多匹配。lstrip()匹配左面移除;rstrip()匹配右面移除;strip()匹配所有移除

    (5) upper:判断是否全是大写;全部转换成大写

    test = "Alex"
    v1 = test.isupper()
    v2 = test.upper()
    print(v1,v2)

    输出结果为:False ALEX
    (6) lower:判断是否全是小写;全部转换成小写

    test = "Alex"
    v1 = test.islower()
    v2 = test.lower()
    print(v1, v2)

    输出结果为:False alex

    (7) replace:将指定字符串替换为指定字符串

    test = "alexalexalex"
    v1= test.replace("ex",'bbb')
    print(v1)
    v2= test.replace("ex",'bbb',2)
    print(v2)

    输出结果为:albbbalbbbalbbb;albbbalbbbalex(2表示"ex"只替换前两次)

    (8)索引,获取字符串中的某一个字符

    test="alex"
    v = test[3]
    print(v)

    输出结果为:x(从0开始计数)

    (9)切片,获取字符串中索引范围的字符

    test="alex"
    v = test[0:3]
    print(v)

    输出结果为:ale(0=<索引范围<3)。若索引范围出现-1表示最后一位

    (10)获取当前字符串由几个字符组成

    test=“王强”
    v=len(test)
    print(v)

    输出结果为:2(用python2运行输出的结果为6,表示由6个字节组成)

    (11)for循环,将字符串中的字符一个一个输出

    test = "泰勒是管理学之父"
    index = 0
    while index < len(test):
        v = test[index]
        print(v)
        index += 1
    print('=======')

    输出结果等同于下面的命令:

    test = "泰勒是管理学之父"
    for wq in test:
    print(wq)

    格式:for 变量名 in 字符串:
         print(变量名)
    break;continue同样适用

    test = "泰勒是管理学之父"
    for item in test:
        continue
    print(item)

    输出结果为:父

    test = "泰勒是管理学之父"
    for item in test:
        print(item)
        break

    输出结果为:泰

    (12)获取连续或不连续的数字,通过设置步长来指定不连续

    v = range(0, 20, 5)
    for item in v:
        print(item)

    输出结果为:

    0
    5
    10
    15

    r1 = range(10)
    r2 = range(1,10)
    r3 = range(1,10,2)
    for a in r1:
        print(a)
    for b in r2:
        print(b)
    for c in r3:
        print(c)

    r1输出为:0,1,2,3,4,5,6,7,8,9(相当于0=<r1<10)

    r2输出为:1,2,3,4,5,6,7,8,9

    r3输出为:1,3,5,7,9(步长为2,依次累加2)

    (13)格式化,将一个字符串中的占位符替换为指定的值

    test = 'i am {name}, age {a}'
    print(test)
    v = test.format(name='alex',a=19)
    print(v)

    输出结果为:

    i am {name}, age {a}
    i am alex, age 19

    6.数字类型里的深度功能

     字符串一旦创建,不可修改;一旦修改或者拼接,都会造成重新生成字符串

    7.在所有的数据类型都可以用的功能:

    len("asdf")
    for循环
    索引
    切片

    8.练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置

    test = input(">>>")
    print(test)
    l = len(test)
    print(l)
    r = range(0,l)
    for item in r:
        print(item, test[item]) 

    此段代码缩写为:

    test = input(">>>")
    for item in range(0, len(test)):
        print(item, test[item])
  • 相关阅读:
    再回首,Java温故知新(八):Java基础之字符串
    《Prism 5.0源码走读》 设计模式
    《Prism 5.0源码走读》Bootstrapper
    VS编译时自动下载NuGet管理的库
    《Prism 5.0源码走读》Prism 5.0简介
    代码阅读
    如何建设个人品牌
    Hexo建站教程
    Codeforce:131A. cAPS lOCK
    一文看懂《最大子序列和问题》
  • 原文地址:https://www.cnblogs.com/quyao/p/9533171.html
Copyright © 2011-2022 走看看