Reputation: 13
I am reading in a list of words from a file. I want to append a string to each word, then print out the word with the appended string. However, my current code appends the string on a new line.
with open("wordDict.txt") as wordFile:
for line in wordFile:
line = line + 'a'
print(line)
An example output I get is this:
hello
a
Output I want to get:
helloa
Any help appreciated
Upvotes: 1
Views: 51
Reputation: 2414
with open("wordDict.txt") as wordFile:
for line in wordFile:
line = line.rstrip() + 'a'
print(line)
should do the trick. rstrip removes trailing whitespace, including newlines.
Upvotes: 2
Reputation: 1454
with open("wordDict.txt") as wordFile:
for line in wordFile:
line = line.strip() + 'a'
print(line)
You need to get rid of the line break at the end of the line. .strip()
gets rid of whitespace at either end of the line.
Upvotes: 3