美文网首页python
Python 字符串常见操作总结

Python 字符串常见操作总结

作者: 混蛋哥 | 来源:发表于2018-05-07 20:41 被阅读46次

介绍Python常见的字符串处理方式

1、字符串连接

# 1、最简单方式 +
>>> 'hello'+' world'
'hello world'
# 2、替换
>>> "%s %s" %('hello',' world')
'hello  world'
# 3、join方法
>>> ' '.join(['hello','world'])
'hello world'

2、字符串转数组

a = 'My name is Jason'
#使用split(str="", num=string.count(str)) 方法根据不同的分割符转,也可指定分割次数,可使用 ' '.join方法转回
>>> 'My name is Jason'.split(' ')
['My', 'name', 'is', 'Jason']
>>> ' '.join(['My', 'name', 'is', 'Jason'])
'My name is Jason'

3、字符串比较

>>> 'hello' == 'hello'
True
>>> 'hello' != 'hello'
False
>>> '123' is 123
False
>>> '123' is not 123
True
# python2中还可以使用cmp,python3改成如下方式
>>> import operator
>>> operator.eq('world',' world')
False

4、字符串查找

#字符串查找
>>> ml = 'Machine Learning'
>>> 'Le' in ml
True
>>> ml.find('n')
5
>>> ml.rfind('n')
14
>>> ml.index('n')
5
>>> ml.rindex('n')
14
>>> ml.find('N')
-1
>>> ml.index('N')
Traceback (most recent call last):
  File "<pyshell#176>", line 1, in <module>
    ml.index('N')
ValueError: substring not found

5、字符串替换

#将字符串中数字替换为空
>>> '123hello'.replace('123','')
'hello'
#正则替换
>>> import re
>>> re.sub("[0-9]",'','123hello')
'hello'

6、字符串首尾匹配

>>> 'cat.jpg'.startswith('cat')
True
>>> 'cat.jpg'.startswith('cat',0,3)
True
>>> 'cat.jpg'.endswith('.jpg')
True
>>> 'cat.jpg'.endswith('.jpg',-4)
True

7、字符串空格处理

>>> s = '  Hello World   '
>>> s.strip()
'Hello World'
>>> s.lstrip()
'Hello World   '
>>> s.rstrip()
'  Hello World'
#扩展
>>> 'www.example.com'.lstrip('www.')
'example.com'
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'

8、字符串格式化、数字及大小写判断、长度补全

#字符串的格式化
>>> '{name},{sex},{age}'.format(age=15,sex='male',name='小安')
'小安,male,15'
>>> '{1},{0},{2}'.format('15','小安','male')
'小安,15,male'
>>> '{},{},{}'.format('小安', '15','male')
'小安,15,male'

#如果字符串中的所有字符都是数字,并且至少有一个字符,则返回真,否则返回假
>>> '123'.isdigit()
True
>>> '123一二三'.isdigit()
False
#isnumeric 是所有字符都是数字字符返回真
>>> '123一二三'.isnumeric()
True

#字符串是否大小写判断
>>> 'abc'.islower()
True
>>> 'Abc'.islower()
False
>>> 'ABC'.isupper()
True

#首字母大写
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
#正则处理方式
>>> import re
>>> def titlecase(s):
...     return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
...                   lambda mo: mo.group(0)[0].upper() +
...                              mo.group(0)[1:].lower(),
...                   s)
...
>>> titlecase("they're bill's friends.")
"They're Bill's Friends."

#返回指定长度字符串,前面补0,一般存csv文件中含00开头的字符0会被抹掉
>>> code = '1'
>>> code.zfill(6)
'000001'

#字符串长度及遍历
>>> s = '混蛋哥'
>>> len(s)
3
>>> for i in s:
    print(i)
混
蛋
哥
>>> 

相关文章

  • Python 字符串常见操作总结

    介绍Python常见的字符串处理方式 1、字符串连接 2、字符串转数组 3、字符串比较 4、字符串查找 5、字符串...

  • Recrod for IT

    常见的字符串处理操作 总结一些常见的字符串处理操作,持续更新1. 求串长2. 串赋值3. 连接操作4. 求子串5....

  • Python常用语法二

    Python 字符串操作和文件操作以及其它Python能力补充 Python字符串操作 in和not in: 'x...

  • 2020-04-26

    Python 入门 学习使用 Python 处理数字与字符串,编写函数和条件语句;操作列表、集合、字典等常见数据类...

  • 09 - 字符串、下标、切片

    #在python表示注释 1.字符串:双引号或者单引号中的数据 字符串输出 字符串输入 字符串常见操作 demo1...

  • Python中的数据类型及常见方法

    Python有六个标准的数据类型: Numbers(数字) 常见操作方法 String(字符串) ...

  • python字符串常见操作

    如果有字符串str = 'zhang zi hao lai zi he nan mei le de dan che...

  • Python 字符串常见操作

    如果有字符串str = 'zi fu chuan chang jian cao zuo' ,一下是常见的操作一、f...

  • python字符串常见操作

    我们一起归总一下python字符串中,常见的一些操作吧 !"find"的用法是表示出索引值所在的位置,用数字表示如...

  • Python字符串常见操作

    一、对字符串的查找 1.find 用于查找字符串中是否含有指定元素,有就返回元素的第一个下标,没有就返回-1。 使...

网友评论

    本文标题:Python 字符串常见操作总结

    本文链接:https://www.haomeiwen.com/subject/zyhvrftx.html