+ 4

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

11th Aug 2020, 2:48 AM
Louai Belfounes
Louai Belfounes - avatar
17 Respuestas
+ 5
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.
11th Aug 2020, 3:43 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
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()
11th Aug 2020, 3:00 AM
Louai Belfounes
Louai Belfounes - avatar
+ 3
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
11th Aug 2020, 3:06 AM
Louai Belfounes
Louai Belfounes - avatar
+ 3
Aymane Boukrouh https://code.sololearn.com/c8ZXkd7e9w6c The file is a word list, in each line, there's a word in alphabetical order.
11th Aug 2020, 3:17 AM
Louai Belfounes
Louai Belfounes - avatar
+ 3
Louai Belfounes The file is the main reason the result is different with or without the strip() method.
11th Aug 2020, 3:25 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 3
Louai Belfounes Also, file.read(), file.readline() and file.readlines() all do different things. So saying they are the same syntax is incorrect.
11th Aug 2020, 3:30 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
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?
11th Aug 2020, 3:13 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
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:
gt; for line in file.readlines(): if len(word)>20: print(word) <$
11th Aug 2020, 3:24 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
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().
11th Aug 2020, 3:41 AM
Louai Belfounes
Louai Belfounes - avatar
+ 1
Aymane Boukrouh You just keep hitting the nail on the head 👌👌
11th Aug 2020, 3:44 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
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.
11th Aug 2020, 3:57 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Aymane Boukrouh Thanks for the advice. I really appreciate.
11th Aug 2020, 3:58 AM
Louai Belfounes
Louai Belfounes - avatar
+ 1
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
11th Aug 2020, 8:00 AM
Jim
Jim - avatar
0
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.
11th Aug 2020, 3:32 AM
Louai Belfounes
Louai Belfounes - avatar
0
Tomiwa Joseph i mean • file = open("words.txt", "r") is the same as : • with open("words.txt", "r") as file
11th Aug 2020, 3:34 AM
Louai Belfounes
Louai Belfounes - avatar
0
Aymane Boukrouh nor am i, when i open the file, i don't see spaces neither the editor shows spaces too. Weird
11th Aug 2020, 3:44 AM
Louai Belfounes
Louai Belfounes - avatar
0
Use line.split()
11th Aug 2020, 11:42 PM
Hansley LOVINCE
Hansley LOVINCE - avatar