Python3基础 str split 用指定的字符将字符串分割

?

  • python : 3.7.0
  • OS : Ubuntu 18.04.1 LTS
  • IDE : PyCharm 2018.2.4
  • conda : 4.5.11
  • type setting : Markdown

?

code

[email protected]:~$ source activate py37
(py37) [email protected]:~$ ipython
Python 3.7.0 (default, Jun 28 2018, 13:15:42)
Type ‘copyright‘, ‘credits‘ or ‘license‘ for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type ‘?‘ for help.

In [1]: content = "hello,world"

In [2]: hello_str = content.split(",")

In [3]: hello_str
Out[3]: [‘hello‘, ‘world‘]

In [4]: hello_str,world_str = content.split(‘,‘, 1)

In [5]: hello_str
Out[5]: ‘hello‘

In [6]: world_str
Out[6]: ‘world‘

In [7]: exit
(py37) [email protected]:~$ source deactivate py37
[email protected]:~$

?

more knowledge

split(self, /, sep=None, maxsplit=-1)
    Return a list of the words in the string, using sep as the delimiter string.

    sep
      The delimiter according which to split the string.
      None (the default value) means split according to any whitespace,
      and discard empty strings from the result.
    maxsplit
      Maximum number of splits to do.
      -1 (the default value) means no limit.

?

[email protected]:~$ source activate py37
(py37) [email protected]:~$ ipython
Python 3.7.0 (default, Jun 28 2018, 13:15:42)
Type ‘copyright‘, ‘credits‘ or ‘license‘ for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type ‘?‘ for help.

In [1]: my_str = "hello,world,hello,world"

In [2]: my_str.split(‘,‘, 1)
Out[2]: [‘hello‘, ‘world,hello,world‘]

In [3]: my_str.split(‘,‘, 2)
Out[3]: [‘hello‘, ‘world‘, ‘hello,world‘]

In [4]: my_str.split(‘,‘, 3)
Out[4]: [‘hello‘, ‘world‘, ‘hello‘, ‘world‘]

In [5]: my_str.split(‘,‘, 4)
Out[5]: [‘hello‘, ‘world‘, ‘hello‘, ‘world‘]

In [6]: exit
(py37) [email protected]:~$ source deactivate py37
[email protected]:~$

?

resource

  • [文档] https://docs.python.org/3/
  • [规范] https://www.python.org/dev/peps/pep-0008/
  • [规范] https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules/
  • [源码] https://www.python.org/downloads/source/
  • [ PEP ] https://www.python.org/dev/peps/
  • [平台] https://www.cnblogs.com/

?



Python具有开源、跨平台、解释型、交互式等特性,值得学习。

Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。

代码的书写要遵守规范,这样有助于沟通和理解。

每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。

原文地址:https://www.cnblogs.com/xingchuxin/p/9696337.html

时间: 2024-08-30 13:15:49

Python3基础 str split 用指定的字符将字符串分割的相关文章

Python3基础 str translate 将指定字符转换成另一种特定字符

? python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 conda : 4.5.11 type setting : Markdown ? code [email protected]:~$ source activate py37 (py37) [email protected]:~$ ipython Python 3.7.0 (default, Jun 28 2018, 13:15:42) Type 'copyright'

python基础--str.split

string = 'This +is -a /string' process = string.split('-') process1 = string.split('-')[-1]#-1和-2可能存在某种特殊意义,小于-2或者大于1就超出list大小来 process2 = string.split('-')[0] process3 = string.split('-')[1] process4 = string.split('-')[-2]# print(process) print(pro

Java基础学习笔记——数学函数、字符和字符串

4.2 常用数学函数 Math类中方法分为三类:三角函数.指数函数方法和服务方法.服务方法包括取整.求最小值.求最大值.求绝对值和随机方法.除了这些方法之外,Math类还提供了两个很有用的double型常量,PI(π)和E(自然对数的底数).可以在任意程序中用Math.PI和Math.E的形式使用这两个变量. 4.2.1 三角函数方法 sin(radians) 返回以弧度位单位的角度的三角正弦函数值 cos(radians) 返回以弧度为单位的角度的三角余弦函数值 tan(randians) 返

Python3基础 read 方法 读取指定txt文件的前几个字符

镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.------------------------------------------ code: # 输入 # 处理 # 输出 #file 文件类型的对象 file=open(r'F:\PersonKey.txt') print(type(file)) print(file) #读文本的前五个字符并打印出来,效果是:<道德经> print(file.read(5)) #这

Python3基础 使用 in notin 查询一个字符是否指定字典的键或者值

镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.------------------------------------------ code: dict1={'子':'鼠','丑':'牛','寅':'虎','卯':'兔','辰':'龙','巳':'蛇','午':'马','未':'羊','申':'猴','酉':'鸡','戌':'狗','亥':'猪'} print('子' in dict1.keys()) print('鼠

Python3基础 str endswith 是否以指定字符串结束

? python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 conda : 4.5.11 type setting : Markdown ? code [email protected]:~$ source activate py37 (py37) [email protected]:~$ ipython Python 3.7.0 (default, Jun 28 2018, 13:15:42) Type 'copyright'

Python3基础 str find+index 是否存在指定字符串,有则返回第一个索引值

? python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 conda : 4.5.11 type setting : Markdown ? code [email protected]:~$ source activate py37 (py37) [email protected]:~$ ipython Python 3.7.0 (default, Jun 28 2018, 13:15:42) Type 'copyright'

Python3基础 str for 输出字符串中的每个字符

? python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 conda : 4.5.11 type setting : Markdown ? code """ @Author : 行初心 @Date : 18-9-23 @Blog : www.cnblogs.com/xingchuxin @GitHub : github.com/GratefulHeartCoder """ de

Python3基础知识之日期时间与字符的转换

问题:"猿类"们都知道,编程中都会涉及到日期.时间类型与字符串类型的转换.不同场景,需要将字符串格式转换为日期类型:也需要将日期类型转换为字符串格式. 目标: 学习和积累python中time和datetime 相关知识: 一.time  引用包time import time 1.获取CTS格式: 星期 月 日 时分秒 年 time.ctime()    结果:Wed May 16 15:12:57 2018 2.字符转换为tuple(time obj) date_string =