Trying to solve new lines in python core | Sololearn: Learn to code for FREE!

0

Trying to solve new lines in python core

Here is my code names = ["John", "Oscar", "Jacob"] file = open("names.txt", "w+") file.write('"John" \n"Oscar" \n"Jacob"') file.close() file= open("names.txt", "r") print(file.read()) file.close() It is outputting the names with double colon when it's not meant to

4/6/2021 1:36:21 PM

Simisola Osinowo

9 Answers

New Answer

+2

names = ["John", "Oscar", "Jacob"] with open("names.txt", "w") as f: for i in names: file.write(i+"\n") with open("names.txt", "r") as f: print(f.read())

+1

names = ["John", "Oscar", "Jacob"] file = open("names.txt", "w") #write down the names into the file for i in range(len(names)): file.write(names[i]) file.close() #Print contents of file line by line file= open("names.txt", "r") print(file.read()) file.close()

+1

names = ["John", "Oscar", "Jacob"] file = open("names.txt", "w") #write down the names into the file for word in names: file.write(word+'\n') file.close() file= open("names.txt", "r") print(file.read()) #output the content of file in console file.close()

0

Line 02: remove single quotation (' ') and use "\n"

0

Thanks, iTech !

0

Used the join() method to get rid of the brackets and quotation marks. Not sure if we're supposed to use the readlines() function, I didn't. Hope this helps. names = ["John", "Oscar", "Jacob"] file = open("names.txt", "w+") names1 = "\n".join(names) file.write(names1) file.close() file.open("names.txt", "r") print(file.read()) file.close()

0

names = ["John", "Oscar", "Jacob"] file = open("names.txt", "w") #write down the names into the file for i in range(len(names)): #Access the names list by i index file.write(names[i] + "\n") file.close() #Print contents of file line by line file= open("names.txt", "r") print(file.read()) file.close()

0

names = ["John", "Oscar", "Jacob"] file = open("names.txt", "w+") file.write('John' '\nOscar' '\nJacob') file.close() file= open("names.txt", "r") print(file.read()) file.close() This is the correct way to write ur code this: file.write('"John" \n"Oscar" \n"Jacob"') is close to correct but wrong its: file.write('John' '\nOscar' '\nJacob')

0

names = ["John", "Oscar", "Jacob"] file = open("names.txt", "w") #write down the names into the file for word in names: file.write(word+'\n') file.close() file= open("names.txt", "r") print(file.read()) #output the content of file in console file.close()