zoukankan      html  css  js  c++  java
  • 字符串类型变量的相关内置函数详解

    # isupper:判断字符串是否全部为大写,返回值为布尔类型,是:true;否:flase

    # str='hello,world'

    # print(str.isupper())

     

    # isdigit:判断字符串是否为整型变量,返回值为布尔类型,是:true;否:false

    # str='hello,world'

    # print(str.isdigit())

    # upper:把字符串全部变为大写

    # str='hello,world'

    # res=str.upper()

    # print(res)

     

    # islower:判断字符串是否全部为小写,返回值为布尔类型, 是:true;否:flase

    # str='hello,world'

    # print(str.islower())

     

    # startswith:判断字符串开头是否为XX字符,(如果是判断多个字符,则必须是连续的字符)

    # 返回值为布尔类型,是:true;否:flase

    # str='hello,world'

    # print (str.startswith('hel'))

    # print (str.startswith('el'))

     

    # endswith:判断字符串结尾是否为XX字符,(如果是判断多个字符,则必须是连续的字符)

    # 返回值为布尔类型,是:true;否:flase

    # str='hello,world'

    # print (str.endswith('hel'))

    # print (str.endswith('ld'))

    # index:索引指定字符,并取下标,从左往右搜索,到第一个字符为止,

    # 编号从0开始,从0开始编号,空格等特殊字符也占一位

    # 如果指定的字符不存在,会报错

    # str='hello,world'

    # print(str.index('d'))

    # print(str.index('l'))

     

    # rindex:索引指定字符,并取下标,从右往左搜索,到第一个字符为止,

    # 编号从左往右,从0开始编号,空格等特殊字符也占一位

    # str='hello,world'

    # print(str.rindex('o'))

    # print(str.index('o'))

    # print(str.rindex('a'))

     

    # find:索引指定字符,并且返回下标,如果没有该字符,则返回-1

    # str='hello,world'

    # print(str.find('e'))

    # print(str.find('a'))

     

    # istitle:判断指定字符是不是title,返回值是布尔类型, 是:true;否:flase

    # title是指单词首字母为大写

    # str='hello,world'

    # print(str.istitle())

     

    # title:将指定字符变为title

    # str='hello,world'

    # print(str.title())

     

    # isalpha:判断字符是否为英文或者汉字,返回值为布尔类型, 是:true;否:flase

    # str='hello world'

    # print(str.isalpha())

    # str='helloworld你好世界'

    # print(str.isalpha())

     

    # count:统计字符串中指定字符出现的个数,返回值为布尔类型,是:true;否:flase

    # 如果字符串中出现空格 特殊字符,则返回值为flase

    # str='hello world'

    # print(str.count('o'))

     

    # isspace:判断字符串是否为空格,返回值为布尔类型,是:true;否:flase

    # str='hello world'

    # print(str.isspace())

    # str='      '

    # print(str.isspace())

     

    # isalnum:判断字符串中是否只包含字符,返回值为布尔类型,是:true;否:flase

    # str='hello world'

    # print(str.isalnum())

    # str='helloworld'

    # print(str.isalnum())

     

    # replace:替换字符串中的指定字符,从左往右替换

    # replace('旧字符','新字符',替换次数)

    # str='hello world'

    # print(str.replace('o','a',1))#只修改一次

    # print(str.replace('o','a',2))#修改两次

    # print(str.replace('o','a'))#不指定修改次数,默认全部修改

    # print(str.replace('o','a',3))#次数超过字符的真实个数,全部修改

     

    #join: 把一个迭代对象变成字符串,该迭代对象中的元素必须是字符

    # 可迭代对象包括  集合,元组,列表,字典,字符串

    # res = ''.join(['a','b','a'])

    # print(['a','b','a'])

    # print(res)

     

    # split:把一个字符串变成列表,

    # split('指定的分隔符',分割次数)

    # str='hello,world,hello,world!'

    # print(str.split(',',2))

    # print(str.rsplit(',',2))

     

    # strip:去除字符串左右两端指定字符

    # str='hello,world,hello,world,hello'

    # print(str.strip('hello'))

    # lstrip:去除左端指定字符

    # str='hello,world,hello,world,hello'

    # print(str.lstrip('hello'))

    # rstrip:去除右端指定字符

    # str='hello,world,hello,world,hello'

    # print(str.rstrip('hello'))

     

    #format将字符串格式化,可以有以下3种格式

    # str1 = 'my name is {},my age is {}'

    # res = str1.format('吉喆', '23')

    # str1 = 'my name is {1},my age is {0}'

    # res = str1.format('23', '李凯')

    # str1 = 'my name is {name},my age is {age}'

    # res = str1.format(name='李凯', age='23')

    # print(res)

     

    #%s,%d,%f可以格式化字符串

    # str1 = 'my name is %s, my age is %s'

    # res = str1 % ('吉喆', 23)

    # print(res)

     

    #利用索引或者下标取值,超出范围报错

    # str1 = 'Hello,World'

    # print(str1[-100])

     

    #字符串的拼接

    # str1 = 'Hello,World'

    # print(str1[4]+str1[5])

    # print('1'+'2')

     

    #切片

    # str1 = 'Hello,World'

    # res = str1[2:5]#正向切片顾头不顾尾

    # print(res)

    # res = str1[-4:-1]#顾尾不顾头

    # print(res)

    # res = str1[:3]#索引为3往右的字符不要了(包括下标为3的字符)

    # print(res)

    # res = str1[3:]#索引为3往左的字符不要了(不包括下标为3的字符)

    # print(res)

    # res = str1[::2]#步长为2,隔一个字符取一个字符

    # print(res)

     

    #三引号和双引号和单引号任意切换,交替使用

    # str1 = ''' "what's your name????" '''

    # print(str1)

    # str1 = "what's your name????"

    # print(str1)

     

  • 相关阅读:
    SEUOJ上几道水题
    项目计划
    软件工程03
    件工程个人作业02
    软件工程个人作业01
    学习进度条
    软件工程第一次博客
    异常分析
    多态
    Java覆盖
  • 原文地址:https://www.cnblogs.com/leeeel/p/10815445.html
Copyright © 2011-2022 走看看