python基础之数据结构

切片:

  

 

 

 

  •   "CHINA"[0:3]   : 输出 CHI,表示从第0位到第二位
  •   "CHINA"[0:0]   : 输出 一个空的字符串
  •   "CHINA"[0:-1]  : 输出 CHIN 表示从左到右的负一位,即 CHIN
  •   "CHINA"[0:]/"CHINA"[:]     : 输出整个字符串, 冒号右边省略表示一直截取到最后一个字符,冒号左边省略表示从第0位即第一位开始截取
  •   "CHINA"[::2]    :  输出CIA 第二个冒号代表的步长, “:2”即代表每隔两个字符截取一个
  •   "CHINA"[::-2]  : 输出AIC  即当冒号后面是负数的时候,表示从右往左截取
  •   "CHINA"[::-1]   : 输出ANIHC 因此当我们想倒序输出一个字符串的时候, 将冒号后面的参数为-1 即为倒叙

  

    

字符串函数:

  Test = " a B c  "

  •   strip().  : strip函数的作用就是去除字符串首尾的所有空白字符。      🌰 Test.strip()  = "a b c"
  •   lstrip()  : lstrip函数的作用就是去除字符串左边的所有空白字符.      🌰 Test.lstrip() = "a b c "

 

  •     rstrip()  :  rstrip函数的作用就是去除字符串右边的所有空白字符.      🌰 Test.rstrip() = " a b c"
  •   upper() :  upper()函数的作用就是将所有的字符变成大写           🌰 Test.upper() = " A B C "
  •     capitalize(): capitalize()函数的作用就是将字符串的首字母变成大写        🌰 Test.capitalize() = " A B c "
  •   lower()  :  lower()函数的作用就是将所有的字符变成小写           🌰 Test.lower() = " a b c "

 

 

  1. startswith :是否以指定的字符串开头   参考🌰:  https://www.runoob.com/python3/python3-string-startswith.html
  2. endswith   : 是否以指定的字符串结尾   参考🌰:  https://www.runoob.com/python3/python3-string-endswith.html
  3. isdigit   : 是否是一串数字             参考🌰:  https://www.runoob.com/python3/python3-string-isdigit.html
  4. islower  : 是否全是小写字母                  参考🌰: https://www.runoob.com/python3/python3-string-islower.html
  5. isupper     : 是否全是大写字母                  参考🌰: https://www.runoob.com/python3/python3-string-isupper.html

字符串函数三

  Test = "This is Test"

  • find() : find()函数的作用是找到字符串中是否存在并返回所在位置的下标         🌰: Test.fing('i')  #结果是   3  如果没有找到会返回-1
  • index() :index()函数的作用是找到字符串中是否存在并返回所在位置的下标.     🌰: Test.index('T') #返回    0  注意⚠️:区别在于没有找到会报错
  • count() : count()函数能够查找出指定的字符串一共出现了几次,               🌰: Test.count('s') # 返回是 3  如果没有出现,则返回0。 
  • replace() :replace()函数提供了替换字符串中某些部分的功能                    🌰: Test.replace('T','t') #返回是 'this is test'
  • len() : len()函数非常重要,它不光可以测量字符串的长度,也可以测量其他所有有长度的对象   
    • 🌰: 
      len("China")   # 5
      len("")           # 0
      len("a")         # 1
      r = range(10)
      len(r)            # 10
       

        

    

 

 

 
     

  

 

 

posted @ 2022-02-24 21:11  TheBlueaa  阅读(33)  评论(0)    收藏  举报