String

Python 2 和Python 3最大的区别就在于字符串

  • Python 2 是Ascii编码,Python 3是Unicode
  • Python 2 Byte序列,Python 3是Unicode序列
  • 字符串是不可变的
  • 字符串支持下标、切片、解包
  • Python 3 接收·Socket·传输过来的Bytes需要手动decode(), Python2则不需要

String基本操作

字符串是一个线性结构

字符串是不可变的结构

s = 'i am LotusChing'
print(s[0])

i

反转字符串

s = 'i am LotusChing'
print(s[::-1])

gnihCsutoL ma i

String格式化

  • % 方法
  • format 方法

% 方法

符号 说明
s 字符串
d 整数
f 浮点数
e 小写科学计数法
E 大写科学计数法

示例

print('i am %s' % ('Da',))
print('i am %(name)s' % {'name':'Da'})
print('i am %(name)s, %(name)s is my first lang' %{'name': 'Da'})

Format 方法

print('{0}, {1}, {2}'.format('a', 'b', 'c'))
print('{}, {}, {}'.format('a', 'b', 'c'))
print('{2}, {1}, {0}'.format('a', 'b', 'c'))
print('{2}, {1}, {0}'.format(*'abc'))
print('{0}{1}{0}'.format('abra', 'cad')

String 常见操作

  • 字符串连接join
  • 字符串分割split rsplit splitlines partition rpartition
  • 字符串大小写capitalize title lower upper swapcase
  • 字符串修改center ljust rjust zfill strip rstrip lstrip
  • 字符串判断count find rfind index replace

String 连接

  • +
  • join

通过+来链接字符串,不得不说,代码很丑

info = ['i','am','LotusChing']
res = ''
for i in info:
    res += i + ' '
print(res)

通过join方法,代码瞬间美观了很多

>>> info = ['I', 'am', 'LotusChing']
>>> info
['I', 'am', 'LotusChing']
>>> ' '.join(info)
'I am LotusChing'

String 分割

split

空格分割,分割2

s = 'I am Lotus Ching'
print(s.split(' ', 2))
['I', 'am', 'Lotus Ching']

使用split来分割K:V结构的配置文件

line = 'ip:1.1.1.1'
>>> k,v = line.split(':',1)
>>> k
'ip'
>>> v
'1.1.1.1'

rsplit

rsplitsplit基本相同,只不过方向是从右向左进行分割

s = 'I am Lotus Ching'
print(s.rsplit(' ',2))
['I am', 'Lotus', 'Ching']

splitlines 以换行符为分隔符

>>> info = '''Hello
I am LotusChing
What 's Your name ?'''
>>> info.splitlines()
['Hello', 'I am LotusChing', "What 's Your name ?"]

partition

partition可以理解为特化的split,有几个不同点

  • partition 会以元组的结构返回分割的内容,包括分隔符
  • partition 默认只分割一次,相当于split(' ',1)
>>> s = 'I am LotusChing'
>>> s.partition()
('I', ' ', 'am LotusChing')

用partition来解析配置文件

>>> line = 'ip:1.1.1.1'
>>> k,_,v=line.partition(':')
>>> k
'ip'
>>> v
'1.1.1.1'

split、partition对比

  • split 更灵活
  • partition 性能更好
>>> s = 'I am LotusChing'
>>> s.split(' ')
['I', 'am', 'LotusChing']
>>> s
'I am LotusChing'
>>> s.partition(' ')
('I', ' ', 'am LotusChing')

split来实现partition(方便对比理解)

def part(s, seq):
    head, tail = s.split(seq, 1)
    return head,seq,tail
s='I am LotusChing'
print(s.partition(' '))
print(part(s,' '))

自然语言处理

capitalize 首字母大写

>>> s='I am LotusChing'
>>> s.capitalize()
'I am lotusching'

lower 全部小写

>>> s.lower()
'i am lotusching'

upper 全部大写

>>> s.upper()
'I AM LOTUSCHING'

title 每个单词首字母大写

>>> s.title()
'I Am Lotusching'

swapcase 大小反转

>>> s.swapcase()
'i AM lOTUScHING'

以上所有修改都是返回一个新的字符串,字符串是不可修改

程序世界的修改

center 居中并使用空格填充两边

>>> s.center(30)
'       I am LotusChing        '
>>> s.center(30,'~')
'~~~~~~~I am LotusChing~~~~~~~~'

ljust 左填充

>>> s.ljust(30)
'I am LotusChing               '
>>> s.ljust(30) + '01'
'I am LotusChing               01'

rjust 右填充

>>> s.rjust(30,'>')
'>>>>>>>>>>>>>>>I am LotusChing'

zfill

>>> s.zfill(30)
'000000000000000I am LotusChing'

strip

>>> s = '   I am LotusChing \n \t'
>>> s.split()
['I', 'am', 'LotusChing']
>>> s = '   I am LotusChing123123'
>>> s.strip('123')
'   I am LotusChing'

rstrip lstrip

  • rstrip 去除右边的空白字符
  • lstrip 去除左边的空白字符
>>> s
'   I am LotusChing \n \t   '
>>> s.lstrip()
'I am LotusChing \n \t   '
>>> s.rstrip()
'   I am LotusChing'

strip还支持连续strip,简直无敌~

>>> s.strip('123').strip(' ')
'I am LotusChing'

字符串判断

startwith 判断开头字符

>>> s
'I am LotusChing'
>>> s.startswith('I')
True
>>> s.endswith('Lotus',5,10)
True

判断uuid

>>> line='UUID=af414ad8-9936-46cd-b074-528854656fcd / ext4 errors=remount-ro 0 1'
>>> uuid='af414ad8-9936-46cd-b074-528854656fcd'
>>> line.startswith(uuid,5)
True

endswith

>>> line='UUID=af414ad8-9936-46cd-b074-528854656fcd / ext4 errors=remount-ro 0 1'
>>> uuid='af414ad8-9936-46cd-b074-528854656fcd'
>>> line.endswith('1')
True

endswith可以用在备用场景

for f in files:
    if f.endswith('.py'):
        backup(f)

isupper

是否是大写

isslower

是否是小写

isdigit 是否是数字

isprintable 是否可打印

String 查找替换

count

>>> s
'I am LotusChing'
>>> s.count(' ')
2

find

>>> s.find(' ')
1
>>> s.find('LotusChing')
5

rfind

>>> s.rfind(' ')
4
>>> s.rfind('LotusChing')
5

index

>>> s.index(' ')
1

rindex

>>> s.rindex(' ')
4

findindex的区别在于查找元素不存时的返回,find返回-1index抛出ValueError异常

>>> s.find('Z')
-1
>>> s.index('Z')
Traceback (most recent call last):
  File "C:\Users\LotusChing\AppData\Local\Programs\Python\Python35\lib\code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
ValueError: substring not found

replace

默认是贪婪替换

>>> s
'abc123abc123'
>>> s.replace('abc', 'xyz')
'xyz123xyz123'

只替换首次匹配

>>> s.replace('abc', 'xyz',1)
'xyz123abc123'

results matching ""

    No results matching ""