美文网首页
Python note_字符串

Python note_字符串

作者: 小浣熊嘎嘣脆 | 来源:发表于2017-10-09 15:18 被阅读0次

字符串方法较多,下面介绍集中常用的字符串方法

  1. find
  2. join
  3. lower
  4. replace
  5. split
  6. strip
  7. translate

1. find

在一个长字符串里查找子串

'little raccoon'.find('cc')
9
'little raccoon'.find('ce')   #如果没有找到则返回-1
-1

还可以设置搜索的起始和终止的位置

'abcdea'.find('a',1)     #从第二位开始寻找a
5
'abcde'.find('c',0,1)  #在第一位到第二位之间寻找c
-1

2. join

连接序列中的元素,而且被连接的序列元素都必须是字符串

dirs='','usr','bin','env'
'/'.join(dirs)
'/usr/bin/env'
print('C:'+'\\'.join(dirs))   #如果想输入/需要进行转译//
C:\usr\bin\env

3. lower

返回字符串的小写字母版

seq = 'ATCCAGCaccgtagcAgcGGtC'
seq.lower()
'atccagcaccgtagcagcggtc'

4. replace

替换

sen = 'This is an apple.'
sen.replace('apple','orange')
'This is an orange.'

5.split (常用)

将字符串分割成序列,是join的逆用法

'a b c d e'.split() #默认空格、制表符、换行符作为分隔
['a', 'b', 'c', 'd', 'e']
'a\tb\tc\td\te'.split()
['a', 'b', 'c', 'd', 'e']
'a1b1c1d1e'.split('1')
['a', 'b', 'c', 'd', 'e']
'a b\tc\td\te'.split() #当字符串里既有空格又有制表符时,默认这两种都会被分隔开
['a', 'b', 'c', 'd', 'e']
'a b\tc\td\te'.split('\t') #当字符串里既有空格又有制表符时,如果只想用\t分隔
['a b', 'c', 'd', 'e']

6. strip

去除字符串两侧(不包括内部)指定的字符,默认删除两端的空格

'1little_raccoon2'.strip('1,2')
'little_raccoon'

7. translate

没太看懂,留个坑

相关文章

  • Python note_字符串

    字符串方法较多,下面介绍集中常用的字符串方法 find join lower replace split stri...

  • Python note_列表

    list函数 列表的基本操作 赋值 删除元素(del) 分片赋值 分片赋值的强大体现在它可以一次为列表中的多个元素...

  • python基础知识(3)

    python字符串 python转义字符 python字符串运算符 python字符串格式化 python格式化操...

  • python count()方法详解

    Python count()方法 Python 字符串 描述 Python count() 方法用于统计字符串里某...

  • python字符串格式化符号与内建函数资料表

    python字符串格式化符号: Python 的字符串内建函数 Python 的字符串常用内建函数如下:

  • 字符串操作方法

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • python字符串相关函数

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 2018-09-28自学习资料

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 字符串内置函数

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 2018-07-18 字符串资料(函数方法)

    Python3字符串资料 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,...

网友评论

      本文标题:Python note_字符串

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