python基础(基本数据类型一)

python基础三(基本数据类型之str)

一、python中常用的数据类型:

  • int(数字)、bool(布尔值)、str(字符串)、tuple(元组)、list(列表)、dict(字典)

二、基础数据类型str

  • 字符串:简单少量的储存数据,并进行相应的操作

  • 字符串的切片和索引

    #字符串的索引取值:索引即下标,就是字符串组成的元素从第一个开始,初始索引为0以此类推
    s='welcom to data types'
    print(s[0])#w
    print(s[6])# :空格
    print(s[20])#超出索引范围会报错
    print(s[-1])#返回s,索引是负数,从后往前检索,最后一个索引值为-1
    print(s[-3])#p
    
    #切片:切片就是通过索引(索引开始值:索引结束值:步长)截取字符串的一段,形成新的字符串(原则就是顾头不顾尾)
    s='welcom to data types'
    print(s[0:3])#wel,不包括索引值为3的元素
    print(s[2:5])#lco
    print(s[0:])#默认到最后,输出welcom to data types
    print(s[0:len(s)])#输出welcom to data types
    print(s[0:-1])#welcom to data type
    print(s[::2])#wlo odt ye
    print(s[0:5:2])#wlo
    #反向切片,第一个值得比第二个大,步长为负
    print(s[5:0:-2])#mce
    
  • 字符串常用方法

    • 1.captalize()方法:首字母大写

      str='this is a school'
      print(str.capitalize())
      #This is a school
      
    • 2.swapcase():大小写翻转

    str1='this Is A school'
    print(str1.swapcase())
    #THIS iS a SCHOOL
    
    • 3.title()方法:每个单词的首字母大写
    str1='this is a school'
    print(str1.title())
    #This Is A School
    
    • 4.upper()方法:将字符串中的小写字母转为大写字母
    str1='this is a school'
    print(str1.upper())
    #THIS IS A SCHOOL
    
    • 5.lower()方法:将字符串中的大写字母转为小写字母
    str1='This A School'
    print(str1.lower())
    #this a school
    
    • 6.center():居中方法
    #center(总字符串个数,不足用传入的字符补齐(默认空格))
    str2='居中'
    print(str2.center(20,'*'))
    #*********居中*********   一共20个字符
    str2='居中'
    print(str2.center(20))#输出20个字符,其中包括18个空格,两边各9个
    #         居中         
    
    • 7.count()方法:数字符串中的元素出现的个数
    str3="this is a teather"
    print(str3.count('i'))
    #输出:2
    print(str3.count('i',0,5))#在索引0-5之间i的个数
    #输出:1
    
    • 8.startswith()方法和endswitch():判断字符串是否以某个字符或字符串开始或结尾
    str4='this is a this '
    print(str4.startswith('this'))#True
    print(str4.startswith('thi s'))#False
    print(str4.endswith('this '))#True
    print(str4.endswith('athis '))#False
    
    • 9.find():寻找字符串中的元素是否存在
    str5='this is a that'
    #找到就返回它的索引值,没找到就返回-1
    print(str5.find('a'))#8
    print(str5.find('an'))#-1
    #在指定索引区间检索
    print(str5.find('is',1,8))#2
    
    • 10.split()方法:以什么分割,最终形成一个列表此列表不含有这个分割的元素
    str5='this is a that'
    print(str5.split(' '))#['this', 'is', 'a', 'that']
    print(str5.split('is'))#['th', ' ', ' a that']以is分割,不包含is
    #不包括该分割元素,返回整个字符串
    print(str5.split('w'))#['this is a that']
    
    • 11.format方法:格式化输出
    # 第一种:默认传参
    res='{} {} {}'.format('tom',18,'岁')
    print(res)#tom 18 岁
    #第二种:索引值
    res='{0} {1} {1}'.format('this','is','that')
    print(res)#this is is
    #第三种:关键字
    res='{name} {age} {sex}'.format(name='tom',age='18',sex='boy')
    print(res)#tom 18 boy
    
    #对比占位符的格式输出
    res='%s is a %s'%('this','apple')
    print(res)#this is a apple
    
    • 12.strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列,字符串中间的空格不可移除
    str6='    this  '
    print(str6.strip())#this
    str7='***this*is*a****'
    print(str7.strip('*'))#this*is*a  只能移除两边的,中间的不能移除
    
    strip()方法:去除左侧空格  
    rstrip()去除右侧空格
    name='**marry**'
    print(name.lstrip('*'))#marry**
    print(name.rstrip("*"))#**marry
    
    • 13.replace("替换的参数","替换为新的参数","替换几个")
    name='tom say:i hava one tell,my name is tom'
    print(name.replace('tom','marry',1))#marry say:i hava one tell,my name is tom
    
    • 14.len()方法:是得到字符串的长度
    name='tom say:i hava one tell,my name is tom'
    print(len(name))#38
    
    • 15.is系列
    #isalnum(): 字符串由字母或数字组成,不空格不属于两者之一
    str1='the number is 666'
    print(str1.isalnum())#False
    str2='thenumberis666'
    print(str2.isalnum())#True
    
    #isalpha():字符串全部是字母组成
    str1='thenumberis666'
    print(str1.isalpha())#False
    str2='thisisaapple'
    print(str2.isalpha())#True
    
    #isdigit():字符串全部数字组成
    str1='6666'
    print(str1.isdigit())#True
    str2='thisis123'
    print(str2.isdigit())#False
    
  • 字符串循环输出

str1='that is'
for i in str1:
    print(i)
#输出:
t
h
a
t
 
i
s    
posted @ 2021-03-08 22:09  辰-月  阅读(202)  评论(0)    收藏  举报