'How to make lower() method work for a read file [duplicate]
In python, I am having a file read then trying to convert everything to lower case but it isn't changing. What am I doing wrong?
fstor = open(txt)
story = fstor.read()
story.lower()
OUTPUT ( I cleaned up punctuations and other marks )
The days went by and the wisest little pig's house took shape brick by brick From time to time his brothers visited him saying with a chuckle Why are you working so hard Why don't you come and play But the stubborn bricklayer pig just said no I shall finish my house first It must be solid and sturdy And then I'll
Solution 1:[1]
Strings are immutable in python
fstor = open("file.txt")
story = fstor.read()
#story.lower() #strings are immutable in python
data = story.lower() #return a new string with lowercase
print(data)
Output:
the days went by and the wisest little pig's house took shape brick by brick from time to time his brothers visited him saying with a chuckle why are you working so hard why don't you come and play but the stubborn bricklayer pig just said no i shall finish my house first it must be solid and sturdy and then i'll
Write the data to another file
with open("file.txt") as fstor:
story = fstor.read()
data = story.lower() #return a new string with lowercase
with open('another_file.txt','w') as afile:
afile.write(data)
Solution 2:[2]
with open('directory_path.txt', 'r') as f:
text = f.read()
text = text.lower()
print(text)
works this code :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ravindu Hirimuthugoda |
