如何在python中将函数参数设置为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38002719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set a function parameter as a string in python?
提问by Vikas Rajasekaran
def count(string,letter):
count=0
for each in string:
if each==letter:
count=count+1
print count
is there a way to set the parameters to automatically accept the inputs as strings so the user doesn't have to add the quotations?
有没有办法设置参数以自动接受作为字符串的输入,这样用户就不必添加引号?
回答by mgilson
I think you're asking if you can call the function like this:
我想你是在问你是否可以这样调用函数:
count(this is my string, i) # 3
instead of like this:
而不是这样:
count('this is my string', 'i') # 3
And the answer is that there is no way to do this. The arguments are parsed when the source code is parsed and the source code parser would have no way to disambiguate a string with commas in it from multiple input arguments.
答案是没有办法做到这一点。在解析源代码时解析参数,并且源代码解析器无法从多个输入参数中消除包含逗号的字符串的歧义。
Of course, this is just one example of parser ambiguity. If a feature like that were to be proposed, I suppose that if you were really strict in the format of the input strings (no commas, extra syntax to disambiguate strings that have variable names from strings that didn't, etc) you could probably come up with a syntax to make it work. However, remembering all of that would be much more difficult than just enclosing your strings in quotes as they're meant to be (and it would make the python parser much more cumbersome to work with). So all in all, I would think it would be a net-loss for the language for them to introduce a feature like that (though I'm just one person who isn't the BDFL so don't mistake my opinion for the opinion of someone who matters).
当然,这只是解析器歧义的一个例子。如果要提出这样的功能,我想如果您对输入字符串的格式非常严格(没有逗号,额外的语法来消除具有变量名称的字符串与没有变量名称的字符串的歧义等),您可能可以想出一个语法来使它工作。然而,记住所有这些比仅仅用引号将字符串括起来要困难得多(这会使 python 解析器使用起来更加麻烦)。总而言之,我认为他们引入这样的功能对语言来说是一种净损失(尽管我只是一个不是 BDFL 的人,所以不要将我的意见误认为是意见重要的人)。
回答by Quentin
Another option:
You can use input()
另一种选择:您可以使用 input()
def count(letter):
string=input() #<- user can type string into command line w/o quote
do_stuff
return bacon
But as far as passing a string as arguments w/o quotes, you can't do that. They'll be interpreted as arguments.
但是就将字符串作为没有引号的参数传递而言,您不能这样做。它们将被解释为参数。
回答by Protocol
This is a complete version to use input()
这是使用input()的完整版本
def count(letter):
string = input() # <- user can type string into command line and end by 'Enter'
count = 0
for each in string:
if each == letter:
count += 1
print (count)
Test Case
测试用例
count('a') # run function 'count'
dsafjqaagreioa # <- input string
4 # <- result. Input string contains 4 'a'
回答by AK47
is there a way to set the parameters to automatically accept the inputs as strings so the user doesn't have to add the quotations?
有没有办法设置参数以自动接受作为字符串的输入,这样用户就不必添加引号?
You must add the quotations so that the interpreter knows the variable is of type String so that the internal code of the method knows how to handle the parameter.
您必须添加引号,以便解释器知道变量是字符串类型,以便方法的内部代码知道如何处理参数。
By default, there is no required type for your parameters for the count method so you could pass a dictionary, integer or list etc to this and it will still run the internal code of the method. However, your code is tailored towards a String variable and a character, so the user must provide the quotations in order for your method to execute as expected otherwise it will most likely result in an Error being raised.
默认情况下,count 方法的参数不需要类型,因此您可以将字典、整数或列表等传递给它,它仍将运行该方法的内部代码。但是,您的代码是针对 String 变量和字符定制的,因此用户必须提供引号,以便您的方法按预期执行,否则很可能会导致引发错误。
def count(string, letter):
ctr = 0
for _letter in string:
if _letter == letter:
ctr += 1
print ctr
回答by galaxyan
def count(string,letter):
if not isinstance(string,str):
string = str(string)
count=0
for each in string:
if each==letter:
count=count+1
print count