Different result with strip(), why ? - Python
Hello, Hope everyone is doing well, I have a question concerning a code I'm writing, that's supposed to print from a file all the worders that have more than 20 characters when I don't use the strip() method it gives me more words and when I use it, it only prints 3 words, why? I'm a beginner with coding in general. Thanks in advance everyone. ----------- My code --------- : def test(): #My steps : #Open the file - DONE #Read the file - DONE #Go through each line of the file - DONE #Read the words line by line - DONE #Identify the words with more than 20 characters - DONE #print the words - DONE file = open("words.txt","r") for line in file : words = line.strip() # HERE !!!!! if len(words) > 20 : print(words): ---------- without strip() ------------ : counterdemonstration counterdemonstrations counterdemonstrators hyperaggressivenesses hypersensitivenesses microminiaturization microminiaturizations representativenesses ----- And with the strip() ------ : counterdemonstrations hyperaggressivenesses microminiaturizations
8/11/2020 2:48:53 AMLouai Belfounes
17 AnswersNew Answer
Louai Belfounes that's why I need to see the file, you said each line contains a word, I didn't think spaces existed as well.
Tomiwa Joseph Yeah, but i want to understand why i'am having two different outputs ? when i add strip(), i don't know exactly which is the expected output i wanted because i don't really get the difference. Ps : rstrip() give the same output as strip()
Tomiwa Joseph the syntax is the same as with open, the question isn't there, the question is in the strip() and with no strip() method
Aymane Boukrouh https://code.sololearn.com/c8ZXkd7e9w6c The file is a word list, in each line, there's a word in alphabetical order.
Louai Belfounes The file is the main reason the result is different with or without the strip() method.
Louai Belfounes Also, file.read(), file.readline() and file.readlines() all do different things. So saying they are the same syntax is incorrect.
Louai Belfounes can you write your code in code playground and share the link? I'm having problems reading it like that because I don't know what you did and what you didn't. Also, what does the file look like?
Louai Belfounes ah, I thought that the steps were actually code. In that case, why use strip at all? If the file only contains one word in each line, you don't need to use strip. Try to change this part of the code instead, maybe it will solve your problem: $> for line in file.readlines(): if len(word)>20: print(word) <$
Aymane Boukrouh i tried something • when i used len() on the words that weren't present in the output with the .strip() in the code : the length was 20, which means it should be printed since the core specify that only words with more than 20 characters should be printed. So basically it did count spaces or blanks as characters when i didn't use strip(). Weird thing, is that i saw no spaces, but i think it's more safer to go with strip().
Aymane Boukrouh You just keep hitting the nail on the head 👌👌
Louai Belfounes I think the best way to learn about files is to create your own custom files and practice on, that way you know exactly what you did wrong or why something didn't go the way it should.
Aymane Boukrouh Thanks for the advice. I really appreciate.
I am not sure, but you don't need strip() you only want to print the words that have more than 20 letters, or you can make an array and say line.strip("/n") #will remove new lines and make an array with all the words inside. Then for element in array: print(element) #this will make the output look a little better in my opinion
Aymane Boukrouh • it's just steps for me when i'am trying to code, i'am a newbie and i thought that may help me structure the problem and resolve it. • Actually, yeah i could just skip the strip(), but i was wondering why it's different ? it must be something there and i'am getting really curious. • Also i think the strip() is useful when we're trying to print the words that has no spaces. • already tried it before, i didn't change anything though.
Tomiwa Joseph i mean • file = open("words.txt", "r") is the same as : • with open("words.txt", "r") as file
Aymane Boukrouh nor am i, when i open the file, i don't see spaces neither the editor shows spaces too. Weird