Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提前解除债务:_Python_String_Helper - Fatal编程技术网

Python 提前解除债务:

Python 提前解除债务:,python,string,helper,Python,String,Helper,如何编写以defbeforePeriod(s)开头的Python函数:该函数返回字符串s中第一个句点之前的子字符串,即参数。字符串包含句点。因此,如果s是“abc2.3ty”,我的函数将返回“abc2” 或 或 如果查看docs.python.org部分,您将看到一个str.find()函数: str.find(sub[,start[,end]]) 返回找到子字符串sub的字符串中的最低索引。。。只有在需要知道子对象的位置时,才应使用find()方法 find() In [2]: def def

如何编写以defbeforePeriod(s)开头的Python函数:该函数返回字符串s中第一个句点之前的子字符串,即参数。字符串包含句点。因此,如果s是“abc2.3ty”,我的函数将返回“abc2”


如果查看docs.python.org部分,您将看到一个
str.find()
函数:

str.find(sub[,start[,end]])
返回找到子字符串sub的字符串中的最低索引。。。只有在需要知道子对象的位置时,才应使用find()方法

find()

In [2]: def defbeforePeriod(s):
   ...:     return s[:s.find('.')]
   ...: 

In [3]: defbeforePeriod('abc2.3ty')
Out[3]: 'abc2'

In [4]: defbeforePeriod('abc2.3ty.zeta')
Out[4]: 'abc2'

如果您试图操纵文件名,请特别注意。

。。。你已经完成了教程的学习了吗?我只是无法理解字符串的
split
方法。我投了反对票,因为这个用户在进入
import
语句之前肯定需要关注标准库。标准库的大部分(除了内置的常量、类型和函数)必须导入。这是python中非常重要的一部分,需要学习。呃,标准库不仅仅是。这就是我的意思。我认为重要的是学习开始学习所有的标准库,这样你就不会重新发明它的一部分了!
def beforePeriod(s):
   return s[s:s.index('.')]
   def beforePeriod(s):
       return s.split('.')[0]
In [2]: def defbeforePeriod(s):
   ...:     return s[:s.find('.')]
   ...: 

In [3]: defbeforePeriod('abc2.3ty')
Out[3]: 'abc2'

In [4]: defbeforePeriod('abc2.3ty.zeta')
Out[4]: 'abc2'