002---Python基本数据类型--字符串

  定义:字符串是一个有序的字符集合,用来存储和表示文本信息。用双引和单引表示。是一种不可变类型。
  
  创建:
  
  In [9]:
  
  s = 'Hello Python'
  
  print(s)
  
  Hello Python
  
  常用操作:
  
  In [1]:
  
  # 索引和切片
  
  s = 'Python'
  
  print(s[1])   # y
  
  print(s[-1])  # n
  
  print(s[1:4]) # yth  顾头不顾尾
  
  y
  
  n
  
  yth
  
  In [2]:
  
  # capitalize()  首字母大写
  
  s = 'hello python'
  
  print(s.capitalize())  # Hello python
  
  Hello python
  
  In [3]:
  
  # upper()  lower()  全大写和全小写
  
  s1 = 'hello python'
  
  s2 = 'HELLO PYTHON'
  
  print(s1.upper())  # Hello python
  
  print(s2.lower())  # hello python
  
  HELLO PYTHON
  
  hello python
  
  In [4]:
  
  # title()  每个隔开(特殊字符和数字)的单词的首字母大写
  
  s = 'life is short you need python'
  
  print(s.title())    # Life Is Short You Need Python
  
  Life Is Short You Need Python
  
  In [11]:
  
  # center()  两边按指定字符填充  居中
  
  s = 'python'
  
  print(s.center(20,'-'))  # -------python-------
  
  -------python-------
  
  In [10]:
  
  #  len()  长度  字符数
  
  s = 'i love you jiang zi ya'
  
  print(len(s))  # 22
  
  s1 = '我喜欢你'
  
  print(len(s1)) # 4
  
  22
  
  4
  
  In [16]:
  
  # startswith(www.quwanyule157.com)  判断是否以什么什么开头  支持指定起始和终止
  
  s = 'jiang ziya'
  
  print(s.startswith('jiang'))     # True
  
  print(s.startswith(www.yongshiyule178.com 'ziya'))      # False
  
  print(s.startswith('jiang',0,5)) # True
  
  print(s.startswith('jiang'www.taohuayuangw.com,0,4)) # False
  
  True
  
  False
  
  True
  
  False
  
  In [19]:
  
  # find()  查找每个字符的索引  没找到返回-1  支持指定起始和终止
  
  s = 'jiang zi ya'
  
  print(s.find('z'))
  
  print(s.find('zi'))
  
  print(s.find('Jiang'))
  
  6
  
  6
  
  -1
  
  In [20]:
  
  # index()  查找每个字符的索引  没找到报错
  
  print(s.index('z'))
  
  print(s.index('zi'))
  
  # print(s.index('Jiang'))
  
  6
  
  6
  
  ---------------------------------------------------------------------------
  
  ValueError                                Traceback (most recent call last)
  
  <ipython-input-20-ff6187789150> in <module>
  
  2 print(s.index('z'))
  
  3 print(s.index('zi'))
  
  ----> 4print(s.index(www.gcyL157.com'Jiang'))
  
  ValueError: substring not found
  
  In [36]:
  
  # strip()  去除两边的指定字符串  默认删空格  rstrip() 左边  lstrip()  右边
  
  s = '    python   '
  
  print(s.strip(www.michenggw.com))
  
  print(s.strip('www.mingcheng178.com/ '))
  
  print(s.strip(' '))
  
  print(''.center(20,'-'))
  
  s1 = '*%jiang*%wei*%%%%'
  
  print(s1.strip('*%'))
  
  python
  
  python
  
  python
  
  --------------------
  
  jiang*%wei
  
  In [46]:
  
  # count() 计算某个字符的个数 支持指定起始和终止位置
  
  s = 'Python python'
  
  print(s.count('t'))
  
  print(s.count('jiang'))
  
  2
  
  0
  
  In [58]:
  
  # split()  按照指定字符切割  默认按照空格切割  返回列表
  
  s = ' i love you '
  
  print(s.split())
  
  print(s.split(' '))
  
  s1 = '--he--he--he--'
  
  print(s1.split('--'))
  
  ['i', 'love', 'you']
  
  ['', 'i', 'love', 'you', '']
  
  [' i love you ']
  
  ['', 'he', 'he', 'he', '']
  
  In [61]:
  
  # replace() 替换 可以指定替换数
  
  s = 'who are you who are you who are you'
  
  print(s.replace('you','she'))
  
  print(s.replace('you','she',2))
  
  who are she who are she who are she
  
  who are she who are she who are you
  
  In [69]:
  
  # is系列
  
  name = 'jiangziya1'
  
  print(name.isalpha())  # 判断字符串是否由字母组成
  
  print(name.isalnum())  # 判断字符串是否由数字和字母组成
  
  print(name.isdigit())  # 判断字符串是否右数字组成
  
  False
  
  True
  
  False

猜你喜欢

转载自my.oschina.net/u/3386278/blog/2980665