简体   繁体   中英

Append int onto string within for loop : Python

I'm trying to append an incremental value onto the end of a string, but the format isn't right, as its printing below it, rather than onto the end. It should be: password1900 password1901 password1902

But its printing as:

password

1900

password

1901

password

1902

Here's what's I've got so far:

fh = open('Password.txt')
OutPut = open("OutPut.txt", 'w')
i = 1900
for line in fh:
    line+=str(i)
    print(line)
    i += 1
fh.close()
OutPut.close()

Try this,

print(line, end=' ')

because in end of print() it takes new line character '\\n' every time after successfully execution. end is default argument in print function which specify which character do you want to keep in end in print()...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM