简体   繁体   English

Python 文档中的 Q:在“string.split(s)”中,为什么是 ( ) 中的 s?

[英]Q on Python Documentation: in "string.split(s)", why s in ( )?

I have recently started learning Python, and I'm having some hard time understand Python Documentation (www.python.org) on string module.我最近开始学习 Python,我很难理解字符串模块上的 Python 文档 (www.python.org)。

When I searched string module using Python Documentation (www.python.org), I get a list of string module functions that are taking an string variable ("s") in the parenthesis followed by the function name.当我使用 Python 文档 (www.python.org) 搜索字符串模块时,我得到了一个字符串模块函数列表,这些函数在括号中包含一个字符串变量 ("s"),后跟函数名称。 For example, in the case of "string.split(s[, sep[, maxsplit]])", shouldn't it be "s.split([sep[, maxsplit]])"?例如,在“string.split(s[, sep[, maxsplit]])”的情况下,不应该是“s.split([sep[, maxsplit]])”吗?

What I don't follow is that the string variable is in the parenthesis like an input variable.我没有遵循的是字符串变量像输入变量一样在括号中。

If s = "I am splitting this sentence.", and I like to split the the sentence (string variable s) by spaces, the command should be s.split().如果 s = "I am splitting this sentence.",并且我喜欢用空格拆分句子(字符串变量 s),则命令应该是 s.split()。 For example:例如:

s = "I am splitting this sentence."

s.split()

['I', 'am', 'splitting', 'this', 'sentence.']

But, when I read the documentation, it sounds like I need to write the command as "split(s)" instead of "s.split()" because the string variable (s) is inside the parenthesis?但是,当我阅读文档时,听起来我需要将命令写为“split(s)”而不是“s.split()”,因为字符串变量 (s) 在括号内?

Can someone please explain why the string varible is inside the parenthesis ("string.split(s[, sep[, maxsplit]])")?有人可以解释为什么字符串变量在括号内(“string.split(s[, sep[, maxsplit]])”)? Why isn't documentation not written as "s.split([sep[, maxsplit]])"?为什么文档没有写成“s.split([sep[, maxsplit]])”?

[Added] Someone below mentioned that my question has been asked and answered on here, but I was not able to find the one. [已添加] 下面有人提到我的问题已在此处提出并回答,但我找不到那个。 And, the question that I was pointed to is not the same question as mine at all... Please help!而且,我被指出的问题与我的问题根本不同......请帮忙!

There is a "string" module, which implements many of the same functions as the string object "str".有一个“string”模块,它实现了许多与字符串对象“str”相同的功能。 Both are valid.两者都有效。 You can do string.split('foo bar baz') or 'foo bar baz'.split() .你可以做string.split('foo bar baz')'foo bar baz'.split()

"The string module contains a number of useful constants and classes, as well as some deprecated legacy functions that are also available as methods on strings." “字符串模块包含许多有用的常量和类,以及一些已弃用的遗留函数,这些函数也可用作字符串的方法。”

It is the way method calls work in Python.这是 Python 中方法调用的工作方式。 Here is example:这是示例:

class Test(object):
 def method(self):
  print 5

Now I can call it two ways:现在我可以通过两种方式调用它:

t = Test()
t.method()

or:或者:

t = Test()
Test.method(t)

So self is just first parameter.所以 self 只是第一个参数。 But first way is strongly preferred.但第一种方式是强烈首选。

s is a string object. s 是一个字符串对象。 Strings have the method split(), and you access it using dot notation iessplit().字符串具有 split() 方法,您可以使用点符号 iessplit() 访问它。 The bracket takes arguments, such as one for selecting what character to split by eg括号接受参数,例如用于选择要拆分的字符的参数,例如

"h.e.l*l.o.".split('*')

will return ['hel','l.o'], because asterisk has been specified as the argument for splitting the string.将返回 ['hel','l.o'],因为星号已被指定为分割字符串的参数。 When you are talking about having s in the brackets in split (split(s)), s represents an argument.当您谈论在 split (split(s)) 中的括号中包含 s 时,s 表示一个参数。 When the s is here: s .split, the s is a string object.当 s 在这里: s .split 时,s 是一个字符串对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM