Reputation: 23
Hello i am a student and i have a few error with my code can anyone help me. The question is to enter enter a list of words and integer number and return the words if the length of the words is more than the integer. This is my answer.
def filter_long_words(string):
string = raw_input("Enter a words : ")
n = raw_input("Enter an integer : ")
count = 0
for letter in string:
count = count + 1
print "The total string are : ", count
return count
filter_long_words(string)
if count > n:
print string
Upvotes: 0
Views: 28577
Reputation: 1
To find the length of the string without counting the spaces
def StrLx_(my_str):
#I am defining a function here for it if you don't want it you can
#delete this part
space = " "
no_space = ""
a = 0
for char in my_str:
if char not in space:
no_space = no_space + char
string = no_space
while string[a] != string[-1]:
a = a+1
print(a+1)
To find the length of the string with spaces getting counted
def StrL_(my_str):
#lly here
a = 0
while my_str[0] != my_str[-1]:
a = a+1
print(a+1)
Upvotes: 0
Reputation: 15236
U can get the length of any string with len()
example:
print (len("string"))
result:
6
Here is a simple example:
In your question you stated that the instruction was to:
return the words if the length of the words is more than the integer.
The below code will do that:
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
In response to your question in the comments:
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
One last example. Lets say you are entering several words as one big string.
We can use .split()
to get each word and test it separately.
# 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)
Results when using 3 as the integer:
Enter an integer: 3
Three
Four
Upvotes: 2
Reputation: 411
You can use len
function to get the length of the string.
i.e.
def filter_long_words():
string = raw_input("Enter a words : ")
n = raw_input("Enter an integer : ")
print ("The total string are : ", len(string))
if len(string) > int(n):
return string
Upvotes: 0
Reputation: 477
I am not sure I understand if you need to check the length of one word or of several words and only keeps the ones that are long enough. Since others have answered for one word, I am answering for several words :
string = raw_input("Enter several words: ")
n = int(raw_input("Enter an integer: "))
def filter_long_words(string, n):
long_words = [word for word in string.split() if len(word) > n]
return long_words
print filter_long_words(string, n)
So, if string = 'one two three four five six'
and n = 3
, the output will be ['three', 'four', 'five']
.
Upvotes: 2
Reputation: 11942
You can get the length of a string with 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)
Upvotes: 1