Justin
Justin

Reputation: 1

How can I put the output into one line?

I wanna put the output into one line like:

It is not a prime number. 
It's common divisors are: 1,2,7,14.

instead of

It is not a prime number. 
It's common divisors are: 
1,2,7,14.

I am a beginner of python.

picture1

Upvotes: 0

Views: 448

Answers (2)

Dani Cuesta
Dani Cuesta

Reputation: 571

Welcome to StackOverflow!

I would say you have two simple options here:

  • Doing every calculation and then printing everything with one print()
  • Using this python3 functionality:

print("test", end = '')

print("Hello ", end = '')
print("world!")
# Output should be: Hello world!

Upvotes: 1

bglbrt
bglbrt

Reputation: 2098

You can actually use the first print function to print everything as one string instead of using the print function twice.

For instance, you could do :

divisors = [1, 2, 7, 14]

divisors_as_string = [str(d) for d in divisors]
print(divisors_as_string)
# > ["1", "2", "7", "14"]

# use the join method to join all strings in your divisors_as_string list
# note that they will be separated by ", " as is specified before in the join method
print("It is not a prime number. It's common divisors are:"+", ".join(divisors_as_string)+".")
# > It is not a prime number. It's common divisors are: 1, 2, 7, 14.

Feel free to ask for any further help, and don't forget to mark the question as answered if you solved your problem!

Upvotes: 1

Related Questions