zoukankan      html  css  js  c++  java
  • 字符串内置方法的使用

    字符串内置方法的使用

    设置字符串:st=‘hello kitty’

    1、统计元素个数

    Print(st.count(‘l’))          #结果为2

    2、首字母大写

    Print(st.capitalize())            #结果为Hello kitty

    3、居中

    Print(st.center(20,’#’))        #结果为####hello kitty#####。参数20为输出的总字符数,参数‘#’为除去st字符串后不足20的用‘#’补齐,并将st字符串处于居中的位置。

    4、判断是否以某个内容开头

    Print(st.startswith(‘he’))           #结果为true。判断字符串st是否以‘he’开头。是则输出‘true’,不是则输出‘false’

    5、查找某个元素在字符串中的索引值

    Print(st.find(‘t’))                       #结果为8。查找从左到右第一个‘t’出现在字符串里的索引值。这种方法每次只能查找一个元素。

    Print(st.rfind(‘t’))                      #结果为9。查找从右到左第一个‘t’出现在字符串里的索引值。这种方法每次只能查找一个元素。

    6、格式化输出

    st1=’{name} is {age}’                           #定义字符串st1

    print(st1.format(ame=’tom’,age=3))  #结果为:tom is 3

    print(st1.format_map({‘name’:’tom’,’age’:3))  #结果为:tom is 3

    7、以小写方式输出

    print(‘MY TITTLE’.lower())        #结果为my tittle。将字符串内的所有大写字母转换成小写输出

    8、以小写方式输出

    print(‘my tittle’.upper())           #结果为MY TITTLE。将字符串内的所有小写字母转换成大写输出

    9、去除字符串的空格、换行符、制表符

    print(’ my tittle ’.strip()) #结果为my tittle。将开头和末尾的空格、换行符、制表符去掉

    print(’ my tittle ’.lstrip()) #结果为my tittle。将开头的空格、换行符、制表符去掉

    print(’ my tittle ’.rstrip())     #结果为my tittle。将末尾的空格、换行符、制表符去掉

    10、替换

    Print(‘my tittle tittle’.replace(‘tittle’,’lesson’,2))    #结果为my lesson lesson。参数‘tittle’为需要被替换的字符,参数‘lesson’是修改为的字符,参数‘2’为修改次数。

    11、分割输出

    Print(‘my tittle tittle’.split( ‘ ‘))   #结果为[‘my’,’tittle’, ’tittle’]。以空格为分割点分割字符串。也可以其他符号为分割点,但必须是字符串内有的。只需更改split的参数即可。也可以设置分割次数,在split的第二个参数设置。

    Print(‘my tittle tittle’.rsplit(‘ ‘,1))#结果为['my tittle', 'tittle']。和split的不同之处是,从右开始,而split是从左开始。其余的两者一样。

    12、判断以某字符串结尾。

    Print(st.endswith(‘y’))         #结果为true。正确则输出true,错误则输出false

    13、判断字符串内是否有特殊字符

    Print(‘as#dk’.isalnum)   #结果为false。因为有‘#’特殊字符。

    Print(‘斯达康dh223’.isalnum)# 结果为true。

    14、判断是否是数字

    Print(‘121’.isdigit())    #结果为true。错误则为false。

    15、判断是否十进制

    Print(‘233’.isdecimal()) #结果为true。错误则为false。

    16、判断是否是每个首字母大写

    Print(‘My Tittle’.istittle( ))#结果为true。错误则为false。

    Print(‘My tittle’.istittle( ))#结果为false.

    17、判断字符串是否全大写

    Print(‘KSDKS’.isupper( ))  #结果为true。错误则为false。

    18、判断字符串是否全小写

    Print(‘hfjhs’.islower( ))              #结果为true。错误则为false。

    19、判断是否是以空格开头

    Print(‘   edkj’.isspace( ))#结果为true。错误则为false。

    20、将大写变小写,小写变大写输出

    Print(‘sdhHD’.swapcase( ))  #结果为SDHhd。

    21、添加字符

    1)join

    a=‘asd’

    b=‘123’

    c=‘ ’.join([a,b])             #拼接a和b得到c

    print (c)                              #结果为asd123

    2)ljust

    Print(‘asd’.ljust(8,’*’)) #结果为asd*****

    3)rjust

    Print(‘asd’.rjust(8,’*’)) #结果为*****asd

  • 相关阅读:
    【Azure 应用服务】在Azure App Service多实例的情况下,如何在应用中通过代码获取到实例名(Instance ID)呢?
    【Azure 应用服务】App Service For Windows 中如何设置代理实现前端静态文件和后端Java Spring Boot Jar包
    【Azure Developer】使用Azure Key Vault 的Key签名后,离线验证的一些参考资料
    【Azure Function】调试 VS Code Javascript Function本地不能运行,报错 Value cannot be null. (Parameter 'provider')问题
    【Azure 应用服务】App Service 使用Tomcat运行Java应用,如何设置前端网页缓存的相应参数呢(Xms512m Xmx1204m)?
    【Azure API 管理】APIM添加Logtoeventhub的策略后,一些相关APIM与Event Hub的问题
    【Azure API 管理】为调用APIM的请求启用Trace 调试APIM Policy的利器
    【Azure 事件中心】China Azure上是否有Kafka服务简答
    【Azure 应用服务】探索在Azure上设置禁止任何人访问App Service的默认域名(Default URL)
    【Azure 微服务】记一次错误的更新Service Fabric 证书而引发的集群崩溃而只能重建
  • 原文地址:https://www.cnblogs.com/xshan/p/7467740.html
Copyright © 2011-2022 走看看