简体   繁体   English

将int附加到for循环内的字符串上:Python

[英]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 它应该是: password1900 password1901 password1902

But its printing as: 但其打印为:

password 密码

1900 1900

password 密码

1901 1901

password 密码

1902 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. 因为在print()的末尾,每次成功执行后,每次都需要换行符'\\ n'。 end is default argument in print function which specify which character do you want to keep in end in print()... end是print函数中的默认参数,它指定要在print()中保留哪个字符...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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