zoukankan      html  css  js  c++  java
  • 二、python沉淀之路~~字符串属性(str)

    1、capitalize的用法:即将输出字符串首字母大写

    1 test = "heLLo"
    2 v = test.capitalize()
    3 print(v)

    结果:Hello。

    2、casefold和lower的用法以及区别

    1 test = "heLLo"
    2 v1 = test.casefold()
    3 print(v1)
    4 v2 = test.lower()
    5 print(v2)

    结果:hello,hello。结果相同,但是适用范围不一样。casefold可以识别世界上大部分国家的 语言转换,而 lower只适用于英语

    3、center的用法

    1 test = "heLLo"
    2 v3 = test.center(20)
    3 print(v3)
    4 v4 = test.center(20,"*")
    5 print(v4)

    结果:

    1        heLLo        
    2 *******heLLo********

    输出设置宽度,并且将字符串放置中间,而且两边可以设置填充物。

    4、count、endswith,startswith三个的用法

    1 test = "helloworldhello"
    2 v = test.count("l")  #统计 l 出现的次数
    3 v1 = test.count("l",3,5)  #在3到5的范围内统计“l”出现的次数
    4 print(v)
    5 v3 = test.endswith("o")  #判断字符串是否已"l"结尾的,是则返回True,否则返回False
    6 v4 = test.endswith("w",2,7)#在2到7的范围内判断是否以"w"结尾
    7 print(v3)
    8 print(v4)
    1 5
    2 True
    3 False

    startswith 的用法与endswith一样

    5、find 和index的用法以及区别

    1 test = "helloworldhello"
    2 v = test.find("w")
    3 v1 = test.find("l")
    4 print(v,v1)
    5 v2 = test.find("l",6,10)
    6 print(v2)
    7 #v3 = test.index("l")
    1 5 2
    2 8

    find和index都是找某个子字符串的位置,而且可以指定范围的寻找。区别在于find找不到时返回-1,index找不到时会报错

    6、format和format_map格式化字符串的用法

    1 test = 'i an {name},age {a}'
    2 test1 = 'i am {0},age{1}'
    3 v = test.format(name="zhongguo",a='18')  #修改内容
    4 v1 = test1.format("xiaoming",'18')  #自动匹配位置
    5 print(v)
    6 print(v1)
    7 v2 = test.format_map({"name":'zhong','a':18})#format_map的用法就是{}里面加字典形式的内容
    8 print(v2)
    1 i an zhongguo,age 18
    2 i am xiaoming,age18
    3 i an zhong,age 18

     7、isalnum()判断一串字符知否只含有字母和数字

    1 test = 'userna1244'
    2 test1 = 'userna1244_'
    3 v = test.isalnum()
    4 print(v)
    5 v1 = test1.isalnum()
    6 print(v1)
    1 True
    2 False

    当被判断字符串内只有字符和数字时 ,返回True,如果哦还是有其他的符号,返回False

    8、expandtabs的用法

    1 test = 'use	rn	a12	44
    use	rn	a12	44
    use	rn	a12	44
    use	rn	a12	44
    '#每段字符串是10个,只要遇到	就空格填充
    2 v = test.expandtabs(10)
    3 print(v)
    1 use       rn        a12       44
    2 use       rn        a12       44
    3 use       rn        a12       44
    4 use       rn        a12       44

    expandtabs和 配合的使用,就会出现以上的输出效果

    9、isalpha的用法

    1 test = "helloworldhello"
    2 test1 = "helloworldhello中国"
    3 test2 = "helloworldhello中国124"
    4 v = test.isalpha()
    5 print(v)
    6 v1 = test1.isalpha()
    7 print(v1)
    8 v2 = test2.isalpha()
    9 print(v2)
    1 True
    2 True
    3 False

    判断一串字符里面是否只有字母和汉字,是则返回True,否则返回False

    10、isdentifier,isdigit,dinumeric三者的用法和区别

     1 test = '1234'
     2 test1 = '1234②'
     3 test2 = '1234②二'
     4 v = test.isdecimal()
     5 v0 = test1.isdecimal()
     6 v00 = test2.isdecimal()
     7 print(v)
     8 print(v0)
     9 print(v00)
    10 v1 = test1.isdigit()
    11 v11 = test2.isdigit()
    12 print(v1)
    13 print(v11)
    14 v2 = test2.isnumeric()
    15 print(v2)
    1 True
    2 False
    3 False
    4 True
    5 False
    6 True

    isdecimal只能判断是否是单纯的数字、isdigit不仅可以判断纯数字,还可以判断特殊序号;isnumeric除了可以判断纯数字和特殊序号外,还可以识别中文数字

    11、isidentifier的用法

     1 test = "helloworld"
     2 test1 = "123helloworld"
     3 test2 = "_helloworld"
     4 test3 = "中国helloworld"
     5 test4 = "*中国helloworld"
     6 v = test.isidentifier()
     7 print(v)
     8 v1 = test1.isidentifier()
     9 print(v1)
    10 v2 = test2.isidentifier()
    11 print(v2)
    12 v3 = test3.isidentifier()
    13 print(v3)
    14 v4 = test4.isidentifier()
    15 print(v4)
    1 True
    2 False
    3 True
    4 True
    5 False

    判断是否是以字母、下划线、中文开头的,是则返回True,否则返回False

    12、isprintable的用法

    1 test = 'aasdjl'
    2 test1 = 'aas	djl'
    3 test2 = 'aas
    djl'
    4 v = test.isprintable()
    5 print(v)
    6 v1 = test1.isprintable()
    7 print(v1)
    8 v2 = test2.isprintable()
    9 print(v2)
    1 True
    2 False
    3 False

    判断是否存在不可显示的字符,没有则返回True ,有则返回False

    13、isspace的用法

    1 test = ''
    2 test1 = ' '
    3 test2 = 'abc abc '
    4 v = test.isspace()
    5 print(v)
    6 v1 = test1.isspace()
    7 print(v1)
    8 v2 = test2.isspace()
    9 print(v2)
    1 False
    2 True
    3 False

    判断字符串是否是空的,是则返回True,否则返回False

    14、istitle和title的用法

    1 test = 'Hello world good morning'
    2 test0 = 'Hello World Good Morning'
    3 v = test.istitle()
    4 print(v)
    5 v0 = test0.istitle()
    6 print(v0)
    7 v1 = test.title()
    8 print(v1)
    1 False
    2 True
    3 Hello World Good Morning

    istitle判断是否是以标题形式书写的字符串,是则返回True,否则返回False,title是将不是标题形式的字符串转换成标题形式的字符串

    15、join 用法【重点】

     1 test = 'Hello world good morning'
     2 test1 = 'Hello'
     3 test2 = 'Hello'
     4 test3 = 'Hello'
     5 v = ' '.join(test)
     6 print(v)
     7 v1 = ' '.join(test1)
     8 print(v1)
     9 v2 = ''.join(test2)
    10 print(v2)
    11 v3 = '*'.join(test3)
    12 print(v3)
    1 H e l l o   w o r l d   g o o d   m o r n i n g
    2 H e l l o
    3 Hello
    4 H*e*l*l*o

    格式化字符串,即占位符

    16、ljust   rjust   center   zfill  四个的用法,对比理解

    1 test = 'Hello'
    2 v = test.ljust(20,'*')
    3 print(v)
    4 v1 = test.rjust(20,'*')
    5 print(v1)
    6 v2 = test.center(20,'*')
    7 print(v2)
    8 v4 = test.zfill(20)
    9 print(v4)
    1 Hello***************
    2 ***************Hello
    3 *******Hello********
    4 000000000000000Hello

    都可以设置长度,前三个都可以指定填充元素;最后一个不能填充元素,只能按照他默认的。

    17、islower  lower  isupper  upper 四个的用法

     1 test = 'HeLLo'
     2 v1 = test.islower()
     3 print(v1)
     4 v2 = test.lower()
     5 print(v2)
     6 print(test)
     7 v3 = test.isupper()
     8 print(v3)
     9 v4 = test.upper()
    10 print(v4)
    1 False
    2 hello
    3 HeLLo
    4 False
    5 HELLO

    18、lstrip     rstrip    strip的用法

    1 test = '    HeLLo    '
    2 v = test.lstrip()
    3 print(v)
    4 v1 = test.rstrip()
    5 print(v1)
    6 v2 = test.strip()
    7 print(v2)
    1 HeLLo    
    2     HeLLo
    3 HeLLo

    分别是去除右边、左边、两边的的空白

    补:

    1 test = 'HeLLoHeLLo'
    2 v = test.lstrip('HeL')
    3 print(v)
    4 v1 = test.rstrip('Lo')
    5 print(v1)
    6 v2 = test.strip('He')
    7 print(v2)
    1 oHeLLo
    2 HeLLoHe
    3 LLoHeLLo

    19、maketrans  translate   的用法

    1 test = 'hello;world;good'
    2 new_test = test.translate(v1)
    3 v1 = str.maketrans('good','1234')
    4 print(v1)
    5 print(new_test)
    1 {103: 49, 111: 51, 100: 52}
    2 hell3;w3rl4;1334

    对应关系替换

    20、partition   rpartiton   splite  rsplite   splitelines  的用法

     1 test = 'helloworld'
     2 v = test.partition('w')
     3 print(v)
     4 v1 = test.rpartition('l')
     5 print(v1)
     6 v2 = test.split('o')
     7 print(v2)
     8 v3 = test.rsplit('o')
     9 print(v3)
    10 test1 = 'hel
    lowor
    ld'
    11 v4 = test1.splitlines()
    12 print(v4)
    1 ('hello', 'w', 'orld')
    2 ('hellowor', 'l', 'd')
    3 ['hell', 'w', 'rld']
    4 ['hell', 'w', 'rld']
    5 ['hel', 'lowor', 'ld']

    partition切出来只有三个元素,split切出来会把指定元素消除。splitlines 是以 为切割标志

    21、swapcase 的用法

    1 test = 'HelloWorlD'
    2 v= test.swapcase()
    3 print(v)
    1 hELLOwORLd

    小写转大写,大写转写

    总结:

    join    split  find   strip  upper   lower  这六个必须掌握

    字符串可以索引,切片,

    也可以用len测试字符串的长度,还可以在for 循环里面被遍历。

  • 相关阅读:
    最详尽的IntelliJ IDEA项目web项目搭建!!!!!!
    Unable to locate JAR/zip in file system as specified by the driver definition: ojdbc14.jar
    Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
    MySQL闪退
    mysql:unknown variable 'default -collation=utf8_general_ci'
    更改文本的编码jsp.xml.java
    save is not valid without active transaction
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptDao_a' defined in class path resource [beansAndHibernate.xml]: Cannot resolve reference to bean 'sessionFact
    java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.LocalSessionFactoryBean
    linux常用Java程序员使用命令(二)
  • 原文地址:https://www.cnblogs.com/jianguo221/p/8921738.html
Copyright © 2011-2022 走看看