Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

converting the last word of a file into uppercase and writing the new content into a new file in Python

Here is the code I tried!
the content in text.txt file is

INPUT

file=open('text.txt','r')
file.seek(0)
a=file.read()
Lst = a.split()
print(Lst)
length=len(Lst)
print(Lst[length-1].upper())
Lst[length-1]).upper()
print(Lst)
#------------------------------#
newfile=open("newfile.txt","w")
newfile.writelines(Lst)
newfile.close()
file.close()

Output
screenshot of the result
as u can see the conversion of uppercase doesn’t show up
and the same output gets written in newfile,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

upper() doesn’t change the element of the list, it just returns the uppercased value. To change it you must assign to the element:

Lst[length-1] = Lst[length-1].upper()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading