For my program I have a function that changes a string into a list however when it hits a newline character it combines the two words on either side of the newline character. Example:
"newline\n problem"
Prints out like this in main function:
print(serperate_words)
newlineproblem
Here is the code:
def stringtolist(lines):
# string of acceptable characters
acceptable = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'’- "
new_string = ''
for i in lines:
# runs through the string and checks to see what characters are in the string
if i in acceptable:
i = i.lower()
# if it is an acceptable character it is added to new string
new_string += i
elif i == '.""':
# if it is a period or quotation marks it is replaced with a space in the new string
new_string += ' '
else:
# for every other character it is removed and not added to new string
new_string += ''
#splits the string into a list
seperate_words = new_string.split(' ')
return seperate_words
for i in lines: if i in acceptable: i = i.lower()will not modify your string. This is because in Python, every name is a reference, so if you assign a reference to something else, the originally referenced object will not change. Methods are the most common way of mutating mutable objects (which btw, strings are not mutable in python).line_break="\n" lines = lines.replace(line_break,"")