How do I set a function parameter as a string in python?
How do I set a function parameter as a string in python?
I think youre 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 didnt, 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 theyre 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 Im just one person who isnt the BDFL so dont mistake my opinion for the opinion of someone who matters).
If you want a parameter as a string or float or etc, try this:
def add(x: float, y: float):
z = x + y
return z
How do I set a function parameter as a string in python?
This is a complete version to use 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