Python中的字符串操作函数split 和 join能够实现字符串和列表之间的简单转换,

  使用 .split()可以将字符串中特定部分以多个字符的形式,存储成列表

 1     def split(self, *args, **kwargs): # real signature unknown
 2         """
 3         Return a list of the words in the string, using sep as the delimiter string.
 4
 5           sep
 6             The delimiter according which to split the string.
 7             None (the default value) means split according to any whitespace,
 8             and discard empty strings from the result.
 9           maxsplit
10             Maximum number of splits to do.
11             -1 (the default value) means no limit.
12         """
13         pass

如上所说,split()函数,使用字符串中的字符作为分隔符(sep),返回字符串分词的列表(不含有作为分隔符的字符),如果分隔符为None,则以字符串中的空格作为分隔符;同时还可以传入一个int参数

作为分隔的次数,(默认值 为 -1,不限制次数)

eg:

>>>:s = 'Process finished with exit code 0'>>>:s.split()# sep:None;maxsplit:-1.空格作为分隔符,分隔所有
['Process', 'finished', 'with', 'exit', 'code', '0']>>>:s.split('i')#sep:‘i’;maxsplit:-1.‘i’作为分隔符,分割所有
['Process f', 'n', 'shed w', 'th ex', 't code 0']>>>:s.split('i', 2)#sep:‘i’;maxsplit:2.‘i’作为分隔符,分割两次
['Process f', 'n', 'shed with exit code 0']

再进一步:

>>>:s = 'Process finished with exiiit code 0'>>>:s.split('i')
['Process f', 'n', 'shed w', 'th ex', '', '', 't   code 0']

  使用join()可以将列表中的字符串类型数据,组合成一个字符串:

 1     def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__
 2         """
 3         Concatenate any number of strings.
 4
 5         The string whose method is called is inserted in between each given string.
 6         The result is returned as a new string.
 7
 8         Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
 9         """
10         pass

如上所说连接任意数量的字符串,将字符串插入到被调用的两两字符串间,返回一个新的字符串。

总结:

s = 'abcdabcd'ls = s.split('c')s2 = 'c'.join(ls)s = s2

上面利用两个函数互相转化,使用split()由字符串得到新列表,再使用join()由列表得到新字符串,

转载于:https://www.cnblogs.com/wangwenhao072093/p/10580414.html

Python中字符串操作函数string.split('str1')和string.join(ls)相关推荐

  1. C#中字符串操作函数

    函数 语法 功能 Len Len(string|varname) 返回字符串内字符的数目,或是存储一变量所需的字节数. Trim Trim(string) 将字符串前后的空格去掉 Ltrim Ltri ...

  2. python中字符串操作

    (1)字符串剔除空格 string.strip()    ---> 去除字符串首尾空格(字符串中间的空格不会被删除) string.lstrip()   ---> 去除字符串首部空格(字符 ...

  3. js中字符串操作函数

    1.concat()  //拼接字符串,不改变原字符串 2.charAt() //返回字符串中指定未知的字符 3.substr() //substr(0,1)  返回从下标为0的字符开始长度为2 的字 ...

  4. python字符串函数split_Python常见字符串操作函数小结【split()、join()、strip()】

    本文实例讲述了Python常见字符串操作函数.分享给大家供大家参考,具体如下: str.split(' ') 1.按某一个字符分割,如'.' >>> s = ('www.google ...

  5. Python中字符串的操作(图文详情)

    以下操作均在pycharm pycharm官方下载地址: https://www.jetbrains.com/pycharm/download/#section=windows 目录 python中字 ...

  6. C++字符串操作函数strstr,strcmp,strlen,strcpy,strcat,strok

    C++中字符串操作函数非常常见,下面是一些常用的str相关操作函数: strstr strstr: 在字符串中查找子字符串 C++ const char* strstr(const char* str ...

  7. python中字符串:声明、编码、函数、格式化

    字符串的声明有三种方式:单引号.双引号和三引号(包括三个单引号或三个双引号).例如: ? 1 2 3 4 5 6 7 8 9 10 11 12 >>> str1= 'hello wo ...

  8. python中查找字符串_python中字符串操作--截取,查找,替换

    python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...

  9. JavaScript中常见的字符串操作函数及用法汇总

    转载地址:http://www.jb51.net/article/65358.htm 这篇文章主要介绍了JavaScript中常见的字符串操作函数及用法,实例汇总了javascript常见的字符串转换 ...

最新文章

  1. 上海松江大学城学计算机,上海松江大学城有几所学校?
  2. python上海培训哪里比较好-上海python培训哪家好 Python需要多久学会
  3. python字符串中strip() 函数和 split() 函数的详解
  4. 编写干净的测试–用特定领域的语言替换断言
  5. UTF-8和GBK有啥区别?
  6. 解决: Cannot find module ‘webpack-cli/bin/config-yargs‘、Error: Cannot find module ‘webpack-cli‘
  7. 朴素贝叶斯法(Naive Bayes,NB)
  8. sql 中 ALTER 和 UPDATE 的区别
  9. Activiti(6.0)任务管理服务TaskaskService
  10. puppet中anchor的作用
  11. java启动脚本_java启动脚本
  12. myeclipse与mysql连接_myeclipse 与 mysql 的连接
  13. kaggle数据集下载
  14. python如何编辑pdf_用Python实现一款永久免费的PDF编辑工具
  15. 杭电oj —— 2023
  16. matlab 效度,量表的信度、效度检验方法小结
  17. 【虚拟机\UBunTu】E45: 已设定选项 ‘readonly‘ (请加 ! 强制执行)
  18. 英语四级和计算机一级算多少学分,英语四级成绩怎么算分 多少分合格
  19. 面试官问我知道的分布式事务,我一口气说了六种
  20. IT狂人职场路:揭秘华为百度高管如何炼成?

热门文章

  1. java发送焦点做移键值_xiaoguozi's Blog
  2. 【图像超分辨率】RS Image SR Based on Visual Saliency Analysis
  3. 数学--数论---欧拉筛 模板
  4. 洛谷P1217 回文质数
  5. 数学--数论--(逆元)扩展欧几里求解+证明
  6. 精心整理吐血推荐的AUTOSAR科普介绍材料
  7. Nand Flash与Nor Flash
  8. ADAS(1) 概述整理及自动驾驶实例
  9. 记录: 开发中的2个线程的使用问题
  10. 【Transformer】ViT:An image is worth 16x16: transformers for image recognition at scale