Uはlen()
例に任意の文字列の長さを得ることができる:
print (len("string"))
結果:
6
ここでは簡単な例である:
であなたの質問は、指示が以下の通りであると述べました。
単語の長さが整数より大きい場合、単語を返します。
def filter_long_words():
my_str = raw_input("Enter a word: ")
n = raw_input("Enter an integer: ")
if len(my_str) > int(n):
return my_str # assigns the results of my_string to some_word
some_word = filter_long_words()
print some_word
最後の一つの例:コメント欄にあなたの質問に応えて
my_str = raw_input("Enter a word: ")
n = raw_input("Enter an integer: ")
def filter_long_words(my_str, n):
if len(my_str) > int(n):
return my_str # assigns the results of my_string to some_word
some_word = filter_long_words(my_str, n)
print some_word
:
以下のコードは、それを行います。 1つの大きな文字列として複数の単語を入力しているとします。
.split()
を使用して各単語を取得し、別々にテストすることができます。
# simulates raw input of 4 words being typed in at once.
list_of_words_as_a_string = "One Two Three Four"
n = raw_input("Enter an integer: ")
def filter_long_words(word, n):
if len(word) > int(n):
print word
return word # this is only useful if you are doing something with the returned word.
for word in list_of_words_as_a_string.split():
filter_long_words(word, n)
結果整数として3を使用して:あなたはlen()
string = raw_input("Enter a words : ")
n = int(raw_input("Enter an integer : ")) # convert the input to integer
if len(string) > n :
print(string)
何が出力されているのですか – 0decimal0
整数を読み込み中に、 'raw_input'の戻り値をキャストします。文字列の長さを取得するには 'len()'を使い、 '=='を使って整数と比較してください。 – Haris
'count'は文字列中の文字の数を返します – 0decimal0