zoukankan      html  css  js  c++  java
  • python自然语言处理——3.2 字符串:最底层的文本处理

    微信公众号:数据运营人
    本系列为博主的读书学习笔记,如需转载请注明出处。

    第三章 加工原料文本

    3.2 字符串:最底层的文本处理

    a = 'Hello'
    b = 'Python'
    s = ' he llo '

    1.字符串连接

    print(a+b)

    HelloPython

    2.重复输出字符串

    print(a*2)

    HelloHello

    3.通过索引获取字符串中的字符

    print(a[1])

    e

    4.截取字符串中的一部分,遵循左闭右开原则

    print(a[1:4])

    ell

    5.成员运算符-如果字符串中包含给定的字符返回True

    'H' in a

    True

    5.成员运算符-如果字符串中不包含给定的字符返回True

    'H' not in a

    False

    6.capitalize()将字符串的第一个字符转换为大写

    print(a.capitalize())

    Hello

    7.zfill(width)返回长度为width的字符串,原字符串右对齐,前面填充0

    print(a.zfill(10))

    '00000Hello'

    8.upper()转换字符串中的小写字母为大写

    print(a.upper())

    'HELLO'

    9.title()返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见istitle())

    print(a.title())

    'Hello'

    10.swapcase()将字符串中大写转换为小写,小写转换为大写

    print(a.title())

    'hELLO'

    11.strip([chars])在字符串上执行 lstrip()和 rstrip()

    s = ' he llo '
    print(s.strip())
    print(s.lstrip())
    print(s.rstrip())

    'he llo'
    'he llo '
    ' he llo'

    12.startswith(substr,beg=0,end=len(string))检查字符串是否是以指定子字符串substr开头,是则返回True,否则返回False。如果beg和end指定值,则在指定范围内检查。

    print(s.startswith('a'))

    False

    13.split(str="",num=string.count(str))num=string.count(str))以str为分隔符截取字符串,如果num有指定值,则仅截取num 个子字符串

    print(s.split('e'))

    [' h', ' llo ']

    14.replace(old,new [,max])把将字符串中的str1 替换成str2,如果max指定,则替换不超过max次。

    print(s.replace('o','y'))

    ' he lly '

    15.max(str)返回字符串str中最大的字母;min(str)返回字符串str中最小的字母。

    print(max(s))
    print(min(s))
    print(ord('o'))  # 查看‘o’对应的ascii码值
    print(ord(' '))

    'o'
    ' '
    111
    32

    16.len(string)返回字符串长度

    print(len(s))

    8

    17.join(seq)以指定字符串作为分隔符,将seq中所有的元素(的字符串表示)合并为一个新的字符串

    print('|'.join(s))

    ' |h|e| |l|l|o| '

    18.字符串判断

    # 如果字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False
    print(s.isalnum())
    # 如果字符串至少有一个字符并且所有字符都是字母则返回True, 否则返回False
    print(s.isalpha())
    # 如果字符串只包含数字则返回True否则返回 False
    print(s.isdigit())
    # 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回True,否则返回False
    print(s.islower())
    # 如果字符串中只包含数字字符,则返回True,否则返回False
    print(s.isnumeric())
    # 如果字符串中只包含空白,则返回True,否则返回False.
    print(s.isspace())
    # 如果字符串是标题化的(见title())则返回 True,否则返回False
    print(s.istitle())
    # 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回True,否则返回False
    print(s.isupper())

    False
    False
    False
    True
    False
    False
    False
    False

    19.find(str,beg=0,end=len(string)) 检测str是否包含在字符串中,如果指定范围beg 和end,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1

    print(s.find('o'))
    print(s.find('y'))

    6
    -1

    20.count(str,beg=0,end=len(string)) 返回str在string里面出现的次数,如果beg或者 end指定则返回指定范围内str出现的次数

    print(s.count('l'))

    2

  • 相关阅读:
    杭电2050
    杭电2043,小细节。。。。。
    杭电2034,坑爹的人见人爱a-b
    杭电2035--人见人爱A^B
    杭电2032--杨辉三角
    杭电2029--Palindromes _easy version(回文串)
    杭电2028--Lowest Common Multiple Plus
    NPOI大数据分批写入同个Excel
    [每日一题] OCP1z0-047 :2013-07-25 权限――角色与对象权限
    Ubuntu下安装搜狗拼音输入法
  • 原文地址:https://www.cnblogs.com/ly803744/p/10426368.html
Copyright © 2011-2022 走看看