Python Question

Unnecessary new lines to the output string, Python, How to get rid of these line breaks?

I have a user.txt with the following contents:

user1:M0024132132

user2:M00123132

user3:M001234123

user4:123123


I wrote a python code to perfom a basic string concatenation operation

filename='user.txt'
separator=":"
fileIN = open(filename, "r")
line = fileIN.readline()

while line:
sout=line.split(separator)
user=sout[0]
passwd=sout[1]
print ('create:' + str(user) + ':' + str(passwd) + ':::' + str(user) + ':/home/' + str(user) + ':/bin/bash:::::' )
line = fileIN.readline()


Expected Output:


create:user:M00123132:::user:/home/user:/bin/bash:::::
create:user1:M00123132:::user1:/home/user1:/bin/bash:::::
create:useruser1:M00212312:::useruser1:/home/useruser1:/bin/bash:::::
create:sdfsdf:12312:::sdfsdf:/home/sdfsdf:/bin/bash:::::


But I'm getting output as:


create:user:M00123132

:::user:/home/user:/bin/bash:::::

create:user1:M00123132

:::user1:/home/user1:/bin/bash:::::

create:useruser1:M00212312

:::useruser1:/home/useruser1:/bin/bash:::::


Kindly help me in solving this.

Answer

In Python 3, you can use the end keyword argument to suppress newlines:

print ('create:' + str(user) + ':' + str(passwd) + ':::' + str(user) 
       + ':/home/' + str(user) + ':/bin/bash:::::', end="")

By the way, you could simply use:

user, passwd = line.split(separator)

instead of using an intermediate list variable (ie. sout).