Python String 字符串

string字符串处理库

字符串作为一种常见的数据类型,在日常中面临各式各样的字符串处理问题,这就要求必须掌握一些常用的字符串处理函数。
字符串的基本需求:查找、修改、删除、判断。

查找

常用于查找到函数有:**find()****index()****count()**

find()

检测字符串是否包含特定字符,如果包含,则返回开始的索引;否则,返回-1。

  1. str='Welcome to DataScience'
  2. print(str.find('Da'))
  3. #11
  4. print(str.find('wc'))
  5. #-1

index()

检测字符串是否包含指定字符,如果包含,则返回开始的索引值;否则,提示错误。

  1. print(str.index('come'))
  2. #3
  3. print(str.index('Date'))
  4. ---------------------------------------------------------------------------
  5. ValueError Traceback (most recent call last)
  6. <ipython-input-12-85801a960777> in <module>
  7. ----> 1 print(str.index('Date'))
  8. ValueError: substring not found

count()

返回str1在string中指定索引范围内[start, end]出现的次数。

  1. print(str.count('e'))
  2. #4
  3. print(str.count('Da'))
  4. #1

修改

常见的用于字符串修改函数有replace()split()join()capitalize()title()upper()loewr()ljust()rjust()center()

replace()

把字符串中的 old(旧字符串)替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

  • 该函数语法如下:
    str.replace(old, new[,max])
  • 参数说明:
    old — 将被替换的子字符串
    new — 新字符串,用于替换old子字符串
  • max — 可选字符串, 替换不超过 max 次

    1. str='Welcome to Data Science'
    2. print(str.replace("Data", "data"))
    3. #Welcome to data Science
    4. print(str.replace("e", "E", 3))
    5. #WElcomE to Data SciEnce

    split()

  • 如果 maxsplit有指定值,则仅分割 maxsplit 个子字符串;

  • 语法格式如下:
    str.split('分界符', maxSplit)
  • 参数说明:
    maxSplit默认值为-1,表示根据定界符分割所有能分割的
  • 返回值为列表

    1. str='Welcome to Data Science'
    2. print(str.split(' ')) #用空格作为分节符
    3. #['Welcome', 'to', 'Data', 'Science']
    4. print(str.split('e',3))
    5. #['W', 'lcom', ' to Data Sci', 'nce']

    capitilize()

  • 将字符串的首字母大写,其余字母全部小写

  • 语法结构如下:
    str.capitiliaze()
  • 无参数

    1. str='welcome to Data Science'
    2. print(str.capitalize())
    3. #Welcome to data science

    join()

  • 用于将序列中的元素以指定的字符连接生成一个新的字符串。

  • 语法结构如下:
    str.join(sequence)
  • sequence — 要连接的元素序列。

    1. seq='Welcome to Data Science'
    2. str='-'
    3. print(str.join(seq))
    4. #W-e-l-c-o-m-e- -t-o- -D-a-t-a- -S-c-i-e-n-c-e

    title()

  • 将字符串中的所有单词的首字母大写,其余字母全部小写;

  • 值得注意的是,这里单词的区分是以任何标点符号区分的,即,标点符号的前后都是一个独立的单词,字符串最后一个标点除外哦。详细看示例哈
  • 语法结构如下:
    str.title()
    无参数

    1. str='welcome to data science'
    2. print(str.title())
    3. #Welcome To Data Science
    4. str='welcome,to data-science.' #加标点符号
    5. print(str.title())
    6. #Welcome,To Data-Science.

    lower()

  • 将字符串的所有字母转换为小写;

  • 语法格式如下:
    str.lower()
    无参数

    1. str='WELCOME TO DATA SCIENCE'
    2. print(str.lower())
    3. #welcome to data science

    upper()

  • 将字符串的所有字母转换为大写;

  • 语法格式如下:

str.upper()
无参数

  1. str='welcome to data science'
  2. print(str.upper())
  3. #WELCOME TO DATA SCIENCE

ljust()

  • 将字符串左对齐,并使用空格填充至指定长度len;
  • 语法格式如下:
    str.ljust(len)

    1. str='Welcome to data science'
    2. print('str的原长度为%d'%(len(str)))
    3. print('str处理后的长度为%d'%(len(str.ljust(30))))
    4. print(str.ljust(30))
    5. '''
    6. str的原长度为23
    7. str处理后的长度为30
    8. Welcome to data science
    9. '''

    rjust()

  • 将字符串右对齐,并使用空格填充至指定长度len

  • 语法格式如下:
    str.rjust(len)

    1. str='Welcome to data science'
    2. print('str的原长度为%d'%(len(str)))
    3. print('str处理后的长度为%d'%(len(str.rjust(30))))
    4. print(str.rjust(30))
    5. #str的原长度为23
    6. #str处理后的长度为30
    7. # Welcome to data science

    center()

  • 将字符串居中,并使用空格填充至指定长度len;

  • 语法格式如下:
    str.center(len)

    1. str='Welcome to Data Science'
    2. print(str.center(30))
    3. print('str的原长度为%d'%(len(str)))
    4. print('str处理后的长度为%d'%(len(str.center(30))))
    5. # Welcome to Data Science
    6. #str的原长度为23
    7. #str处理后的长度为30

    删除

    常用的字符串删除方法有:**lstrip()****rstripe()****stripe()**

    lstrip()

  • 去掉字符串左边的空白字符;

  • 语法格式如下:
    str.lstrip()

    1. str=' Welcome to Data Science'
    2. print(str)
    3. print(str.lstrip())
    4. # Welcome to Data Science
    5. #Welcome to Data Science

    rstrip()

  • 去掉字符串右边的空白字符;

  • 语法格式如下:
    str.rstrip()

    1. str='Welcome to Data Science '
    2. print(str)
    3. print(str.rstrip())
    4. #Welcome to Data Science
    5. #Welcome to Data Science

    strip()

  • 去掉字符串两边的空白字符;

  • 语法格式如下:
    str.strip()

    1. str=' Welcome to Data Science '
    2. print(str)
    3. print(str.strip())
    4. # Welcome to Data Science
    5. #Welcome to Data Science

    判断

    常见的用于判断字符串的函数有:startwith()endwith()isalpha()isdigit()isalnum()isspace()

    startswith()

  • 检查字符串str是否 以字符串str1开头,若是,则返回True;否则,返回False;

  • 语法结构如下:
    str.startswith()

    1. str='Welcome to Data Science'
    2. print(str.startswith('W'))
    3. print(str.startswith('w'))
    4. #True
    5. #False

    endswith()

  • 检查字符串str是否 以字符串str1结尾,若是,则返回True;否则,返回False;

  • 语法结构如下:
    str.endswith()

    1. str='Welcome to Data Science'
    2. print(str.endswith('e'))
    3. print(str.endswith('c'))
    4. #True
    5. #False

    isalpha()

  • 如果字符串str中只包含字母,则返回True;否则,返回False;

  • 语法结构如下:
    str.isalpha()

    1. str='Welcome to Data Science'
    2. print(str.isalpha()) #包含空格
    3. #False
    4. str_='asd'
    5. print(str_.isalpha())
    6. #True

    isdigit()

  • 如果字符串str中只包含数字,则返回True;否则,返回False;

  • 语法结构如下:
    str.isdigit()

    1. str='Welcome to Data Science'
    2. print(str.isdigit())
    3. #False
    4. str_='12345'
    5. print(str_.isdigit())
    6. #True

    isalnum()

  • 检测字符串是否由字母和数字组成。

  • 语法结构如下:
    str.isalnum()

    1. str='Welcome to Data Science'
    2. print(str.isalnum())
    3. #False
    4. str_='123data'
    5. print(str_.isalnum())
    6. #True

    isspace()

  • 如果字符串str中只包含空格,则返回True;否则,返回False;

  • 语法结构如下:
    str.isspace()
    1. str='Welcome to Data Science'
    2. print(str.isspace())
    3. #False